-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSolution283.java
More file actions
36 lines (30 loc) · 766 Bytes
/
Solution283.java
File metadata and controls
36 lines (30 loc) · 766 Bytes
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
package algorithm.leetcode;
/**
* @author: mayuan
* @desc: 移动零
* @date: 2019/01/04
*/
public class Solution283 {
public static void main(String[] args) {
int[] numbers = {0, 1, 0, 3, 12};
new Solution283().moveZeroes(numbers);
for (int i = 0; i < numbers.length; ++i) {
System.out.print(numbers[i]);
System.out.print(" ");
}
}
public void moveZeroes(int[] nums) {
if (null == nums || 1 >= nums.length) {
return;
}
int i = 0, j = 0;
for (; j < nums.length; ++j) {
if (0 != nums[j]) {
nums[i++] = nums[j];
}
}
for (; i < nums.length; ++i) {
nums[i] = 0;
}
}
}