-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path0283.移动零.java
67 lines (64 loc) · 1.14 KB
/
0283.移动零.java
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* @lc app=leetcode.cn id=283 lang=java
*
* [283] 移动零
*
* https://leetcode.cn/problems/move-zeroes/description/
*
* algorithms
* Easy (64.03%)
* Likes: 1627
* Dislikes: 0
* Total Accepted: 780.2K
* Total Submissions: 1.2M
* Testcase Example: '[0,1,0,3,12]'
*
* 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
*
* 请注意 ,必须在不复制数组的情况下原地对数组进行操作。
*
*
*
* 示例 1:
*
*
* 输入: nums = [0,1,0,3,12]
* 输出: [1,3,12,0,0]
*
*
* 示例 2:
*
*
* 输入: nums = [0]
* 输出: [0]
*
*
*
* 提示:
*
*
*
* 1 <= nums.length <= 10^4
* -2^31 <= nums[i] <= 2^31 - 1
*
*
*
*
* 进阶:你能尽量减少完成的操作次数吗?
*
*/
// @lc code=start
class Solution {
public void moveZeroes(int[] nums) {
int i = 0, j = 0;
while (j < nums.length) {
if (nums[j] != 0)
nums[i++] = nums[j];
j++;
}
while (i < nums.length) {
nums[i++] = 0;
}
}
}
// @lc code=end