-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path283. Move Zeroes
52 lines (42 loc) · 949 Bytes
/
283. Move Zeroes
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
51
52
#SOLUTION-1
Runtime 1 ms
Beats 100%
Memory 45.2 MB
Beats 59.47%
class Solution {
public void moveZeroes(int[] nums) {
int i = 0;
for (int num:nums){
if(num != 0){
nums[i] = num;
i++;
}
}
while(i<nums.length){
nums[i] = 0;
i++;
}
}
}
#SOLUTION-2
Runtime 2 ms
Beats 40.49%
Memory 44.8 MB
Beats 83.35%
class Solution {
public void moveZeroes(int[] nums) {
int []target=new int[nums.length];
System.arraycopy(nums, 0, target, 0, nums.length);
int beg = 0 , end = nums.length - 1;
for(int i=0 ; i < nums.length && end >= beg ; i++){
if(target[i] == 0){
nums[end]=target[i];
end--;
}
else{
nums[beg]=target[i];
beg++;
}
}
}
}