-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode1470.java
More file actions
32 lines (28 loc) · 1.02 KB
/
LeetCode1470.java
File metadata and controls
32 lines (28 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.Arrays;
public class LeetCode1470 {
public static void main(String[] args) {
// 输入:nums = [2,5,1,3,4,7], n = 3
// 输出:[2,3,5,4,1,7]
System.out.println(Arrays.toString(new Solution1470().shuffle(new int[] { 2, 5, 1, 3, 4, 7 }, 3)));
// 输入:nums = [1,2,3,4,4,3,2,1], n = 4
// 输出:[1,4,2,3,3,2,4,1]
System.out.println(Arrays.toString(new Solution1470().shuffle(new int[] { 1, 2, 3, 4, 4, 3, 2, 1 }, 4)));
// 输入:nums = [1,1,2,2], n = 2
// 输出:[1,2,1,2]
System.out.println(Arrays.toString(new Solution1470().shuffle(new int[] { 1, 1, 2, 2 }, 2)));
}
}
class Solution1470 {
public int[] shuffle(int[] nums, int n) {
int[] newNums = new int[2 * n];
int left = 0;
int right = n;
int index = 0;
while (index < nums.length) {
newNums[index] = nums[left++];
newNums[index + 1] = nums[right++];
index += 2;
}
return newNums;
}
}