Skip to content

Commit fde398d

Browse files
committed
fd
1 parent 8b1899d commit fde398d

File tree

3 files changed

+28
-58
lines changed

3 files changed

+28
-58
lines changed

solution/src/main/java/com/inuker/solution/MoveZeroes.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
*/
1010
public class MoveZeroes {
1111

12+
/**
13+
* 要保持顺序,写的次数最少
14+
*/
1215
public void moveZeroes(int[] nums) {
1316
for (int i = 0, j = 0; j < nums.length; j++) {
1417
if (nums[j] != 0) {
@@ -24,24 +27,17 @@ private void swap(int[] nums, int i, int j) {
2427
}
2528

2629
/**
27-
* 要求操作次数最少,如果不要求保持顺序,无需理会超出的部分,所以这里不用swap,另外当i==j时就不要多余地操作一次了
28-
* 最后返回有效部分的长度
30+
* 如果不要求保持顺序,且写的次数最少
2931
*/
3032
public int moveZeroes2(int[] nums) {
31-
int i = 0, j = nums.length - 1;
32-
while (i <= j) {
33+
for (int i = 0, j = nums.length - 1; j > i; ) {
3334
if (nums[i] != 0) {
3435
i++;
35-
} else if (nums[j] == 0) {
36-
j--;
36+
} else if (nums[j] != 0) {
37+
swap(nums, i, j--);
3738
} else {
38-
if (i != j) {
39-
nums[i] = nums[j];
40-
}
41-
i++;
4239
j--;
4340
}
4441
}
45-
return i;
4642
}
4743
}
Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.inuker.test;
22

3+
import com.leetcode.library.TreeNode;
4+
35
import java.util.ArrayList;
46
import java.util.Arrays;
57
import java.util.Collections;
@@ -20,45 +22,22 @@
2022

2123
public class Test2 {
2224

23-
public int[] searchRange(int[] nums, int target) {
24-
if (nums.length == 0) {
25-
return new int[]{-1, -1};
26-
}
27-
return new int[] {
28-
lowerBound(nums, target),
29-
upperBound(nums, target)
30-
};
31-
}
32-
33-
public static int lowerBound(int[] nums, int target) {
34-
int left = 0, right = nums.length - 1;
35-
36-
while (left < right) {
37-
int mid = left + ((right - left) >> 1);
38-
39-
if (target > nums[mid]) {
40-
left = mid + 1;
25+
public void moveZeroes(int[] nums) {
26+
for (int i = 0, j = nums.length - 1; j > i; ) {
27+
if (nums[i] != 0) {
28+
i++;
29+
} else if (nums[j] != 0) {
30+
swap(nums, i, j--);
4131
} else {
42-
right = mid;
32+
j--;
4333
}
4434
}
45-
46-
return nums[left] == target ? left : -1;
4735
}
4836

49-
public static int upperBound(int[] nums, int target) {
50-
int left = 0, right = nums.length - 1;
51-
52-
while (left < right) {
53-
int mid = left + ((right - left) >> 1) + 1;
54-
55-
if (target < nums[mid]) {
56-
right = mid - 1;
57-
} else {
58-
left = mid;
59-
}
60-
}
61-
62-
return nums[right] == target ? right : -1;
37+
private void swap(int[] nums, int i, int j) {
38+
int t = nums[i];
39+
nums[i] = nums[j];
40+
nums[j] = t;
6341
}
42+
6443
}

test/src/main/java/com/inuker/test/main.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,13 @@
2828
public class main {
2929

3030
public static void main(String[] args) {
31-
System.out.println(getTag("abc"));
32-
System.out.println(getTag("bcd"));
33-
System.out.println(getTag("cde"));
31+
int[] arr = new int[] {
32+
0, 0, 3, 4, 5, 0, 0
33+
};
34+
new Test2().moveZeroes(arr);
35+
for (int n : arr) {
36+
System.out.print(n + " ");
37+
}
3438
}
3539

36-
static String getTag(String s) {
37-
int diff = (int) s.charAt(0) - (int) 'a';
38-
39-
StringBuilder sb = new StringBuilder();
40-
for (char c : s.toCharArray())
41-
sb.append((c + 26 - diff) % 26);
42-
43-
return sb.toString();
44-
}
4540
}

0 commit comments

Comments
 (0)