-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode2149failed.java
More file actions
50 lines (48 loc) · 1.39 KB
/
leetcode2149failed.java
File metadata and controls
50 lines (48 loc) · 1.39 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.util.Arrays;
public class leetcode2149failed {
public static int[] rearrangeArray(int[] nums) {
int n = nums.length;
int x = 0;
int current = nums[x];
int nextNum = 0;
while(x<n){
if(x%2==0){
if(current>=0){
x++;
continue;
}
else{
for(int i = x ; i< n; i++){
if(nums[i]>=0){
int temp = nums[i];
nums[i] = current;
current = temp;
}
}
x++;
}
} else if (x%2!=0) {
if(current<0){
x++;
continue;
}
else{
for(int i = x ; i< n; i++){
if(nums[i]<0){
int temp = nums[i];
nums[i] = current;
current = temp;
}
}
x++;
}
}
}
return nums;
}
public static void main(String[] args) {
int[]nums = {3,1,-2,-5,2,-4};
int[] solution = rearrangeArray(nums);
System.out.println(Arrays.toString(solution));
}
}