-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveZeros.java
More file actions
48 lines (39 loc) · 1.06 KB
/
MoveZeros.java
File metadata and controls
48 lines (39 loc) · 1.06 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
package OneDimensionArray;
/**
* Created by devpriyadave on 4/4/18.
*/
public class MoveZeros {
public void moveZeroes(int[] nums) {
int x = 1;
for(int i=0; i< nums.length; i++) {
if(nums[i] == 0) {
while(i+x < nums.length && nums[i+x]==0 ) {
x++;
}
if(i+x >= nums.length) {
break;
}
nums[i] = nums[i+x];
nums[i+x] = 0;
}
}
System.out.println("here");
}
public class Solution {
public void moveZeroes(int[] nums) {
int j = 0;
for(int i = 0; i < nums.length; i++) {
if(nums[i] != 0) {
int temp = nums[j];
nums[j] = nums[i];
nums[i] = temp;
j++;
}
}
}
}
public static void main(String[] args) {
MoveZeros moveZeros = new MoveZeros();
moveZeros.moveZeroes(new int[]{0, 0});
}
}