-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.java
35 lines (32 loc) · 843 Bytes
/
Main.java
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
33
34
35
package com.company;
public class Main {
public static void main(String[] args) {
int[] nums = {2,5,1,3,4,7};
int n = 3;
shuffle(nums, n);
}
public static int[] shuffle(int[] nums, int n) {
int[] res = new int[nums.length];
int p1 = 0, p2 = 1;
for (int i = 0; i < nums.length; i++) {
if(i < n){
res[p1] = nums[i];
p1 += 2;
}
else{
res[p2] = nums[i];
p2 += 2;
}
}
return res;
}
public static int[] shuffle2(int[] nums, int n) {
int[] res = new int[nums.length];
int index = 0;
for (int i = 0; i < n; i++) {
res[index++] = nums[i];
res[index++] = nums[i + n];
}
return res;
}
}