题目描述:
给你一个长度为 n 的整数数组 nums 。请你构建一个长度为 2n 的答案数组 ans ,数组下标 从 0 开始计数 ,对于所有 0 <= i < n 的 i ,满足下述所有要求:
- ans == nums
- ans[i + n] == nums
具体而言,ans 由两个 nums 数组 串联 形成。
返回数组ans 。
示例 1:
- <strong>输入:</strong>nums = [1,2,1]
- <strong>输出:</strong>[1,2,1,1,2,1]
- <strong>解释:</strong>数组 ans 按下述方式形成:
- - ans = [nums[0],nums[1],nums[2],nums[0],nums[1],nums[2]]
- - ans = [1,2,1,1,2,1]
复制代码 示例 2:
- <strong>输入:</strong>nums = [1,3,2,1]
- <strong>输出:</strong>[1,3,2,1,1,3,2,1]
- <strong>解释:</strong>数组 ans 按下述方式形成:
- - ans = [nums[0],nums[1],nums[2],nums[3],nums[0],nums[1],nums[2],nums[3]]
- - ans = [1,3,2,1,1,3,2,1]
复制代码 上代码拿去即可运行:
- package suanfa;
- public class Test01 {
- public static void main(String[] args) {
- int[]operations1={1,3,2,1};
- int[] arrayResult = new int[operations1.length*2];
- for (int i : sumTwo(operations1, arrayResult)) {
- System.out.print(i+" " ) ;
- }
- }
-
- public static int[] sumTwo(int[] array,int[] arrayResult) {
- Integer length = array.length;
- for (int i = 0; i < length; i++) {
- arrayResult[length+i]=array[i];
- arrayResult[i]=array[i];
- }
- return arrayResult;
- }
- }
复制代码
运行结果:
我要刷300道算法题,第114道 。 很久很久没写算法了,今天开始写,先从最简朴的开始。希望自己可以对峙下去。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |