-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMoveZeroes.java
More file actions
49 lines (44 loc) · 1.16 KB
/
MoveZeroes.java
File metadata and controls
49 lines (44 loc) · 1.16 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
package array;
import java.util.LinkedList;
import java.util.Queue;
// Source : https://leetcode.com/problems/move-zeroes/
// Id : 283
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2019-06-10
// Topic : Array
// Level : Easy
// Other :
// Tips :
// Result : 100.00% 94.20%
public class MoveZeroes {
//40.88% 1 ms 99.93%
public void moveZeroesOrigin(int[] nums) {
Queue<Integer> zeros = new LinkedList<>();
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 0) {
if (zeros.isEmpty())
continue;
else {
nums[zeros.poll()] = nums[i];
nums[i] = 0;
zeros.add(i);
}
} else {
zeros.add(i);
}
}
}
//100.00% 0ms 94.20%
public void moveZeroes(int[] nums) {
int k = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 0) {
nums[k] = nums[i];
k++;
}
}
for (int i = k; i < nums.length; i++) {
nums[i] = 0;
}
}
}