-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode26.java
More file actions
43 lines (38 loc) · 1.2 KB
/
LeetCode26.java
File metadata and controls
43 lines (38 loc) · 1.2 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
import java.util.Arrays;
import util.PrintUtil;
public class LeetCode26 {
public static void main(String[] args) {
int[] nums;
int N;
// 输入:nums = [1,1,2]
// 输出:2, nums = [1,2,_]
nums = new int[] { 1, 1, 2 };
System.out.println(Arrays.toString(nums));
N = new Solution26().removeDuplicates(nums);
System.out.println(Arrays.toString(Arrays.copyOf(nums, N)));
PrintUtil.printDivider();
// 输入:nums = [0,0,1,1,1,2,2,3,3,4]
// 输出:5, nums = [0,1,2,3,4]
nums = new int[] { 0, 0, 1, 1, 1, 2, 2, 3, 3, 4 };
System.out.println(Arrays.toString(nums));
N = new Solution26().removeDuplicates(nums);
System.out.println(Arrays.toString(Arrays.copyOf(nums, N)));
PrintUtil.printDivider();
}
}
class Solution26 {
public int removeDuplicates(int[] nums) {
int slow = 0;
int fast = 1;
while (fast < nums.length) {
if (nums[slow] != nums[fast]) {
nums[slow + 1] = nums[fast];
fast++;
slow++;
} else {
fast++;
}
}
return slow + 1;
}
}