We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e411be6 commit 38e2f59Copy full SHA for 38e2f59
283.移动零.java
@@ -32,18 +32,26 @@
32
// @lc code=start
33
class Solution {
34
public void moveZeroes(int[] nums) {
35
- int count = 0;
+ int i0 = 0;
36
+ boolean find0 = true;
37
for (int i = 0; i < nums.length; i++) {
- if (nums[i] == 0) {
38
- count++;
39
- } else {
40
- if (count > 0) {
41
- nums[i - count] = nums[i];
42
- nums[i] = 0;
43
- }
+ if (find0 && nums[i] == 0) {
+ i0 = i;
+ find0 = false;
+ continue;
+ }
+ if (!find0 && nums[i] != 0) {
44
+ swap(nums, i, i0);
45
+ i0++;
46
}
47
48
49
+
50
+ private void swap(int[] nums, int i, int j) {
51
+ int tmp = nums[i];
52
+ nums[i] = nums[j];
53
+ nums[j] = tmp;
54
55
56
// @lc code=end
57
0 commit comments