-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray283.java
More file actions
50 lines (49 loc) · 1.31 KB
/
Array283.java
File metadata and controls
50 lines (49 loc) · 1.31 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
package array;
/**
* @ProjectName: leetcode
* @Package: array
* @ClassName: Array283
* @Author: markey
* @Description:
* 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
*
* 示例:
*
* 输入: [0,1,0,3,12]
* 输出: [1,3,12,0,0]
* 说明:
*
* 必须在原数组上操作,不能拷贝额外的数组。
* 尽量减少操作次数。
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/move-zeroes
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
* @Date: 2020/2/8 16:14
* @Version: 1.0
*/
public class Array283 {
public static void main(String[] args) {
moveZeroes(new int[] {1, 0});
}
public static void moveZeroes(int[] nums) {
int i = 0, j = 1;
while (i < nums.length) {
if (nums[i] == 0) {
while (j < nums.length && nums[j] == 0) {
j++;
}
if (j >= nums.length) {
break;
}
nums[i] = nums[j];
nums[j] = 0;
}
i++;
j = i+1;
}
for (int k = 0; k < nums.length; k++) {
System.out.println(nums[k]);
}
}
}