-
Notifications
You must be signed in to change notification settings - Fork 6
/
_26_Remove_Duplicates_from_Sorted_Array.java
72 lines (66 loc) · 1.92 KB
/
_26_Remove_Duplicates_from_Sorted_Array.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
68
69
70
71
72
package leetcode;
import java.util.Arrays;
/**
* Created by pingcai on 17-7-7.
*/
public class _26_Remove_Duplicates_from_Sorted_Array {
public static void main(String[] args) {
int[] arr = {1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5, 5, 5, 5, 5};
//int[] arr = {1,2};
System.out.println(new _26_Remove_Duplicates_from_Sorted_Array().removeDuplicates3(arr));
System.out.println(Arrays.toString(arr));
}
/**
* 本题的要点是:数组已经排好序,需要做的是用一个 p 记录相同元素间的间隔
* 当碰到下一个不相同的元素nums[i]时,将其移动到nums[i-p]
*/
public int removeDuplicates(int[] nums) {
int len = 0;
int p = 0;
int i = 0;
if (nums != null && nums.length > 1) {
while (i < nums.length) {
if (i > 0 && nums[i] == nums[i - 1]) {
p++;
} else {
len++;
nums[i - p] = nums[i];
}
i++;
}
} else if (nums.length == 1) {
len = 1;
}
return len;
}
/**
* 另一种写法,直接用一个变量记住可以替换的位置
*/
public int removeDuplicates2(int[] nums) {
int len = 0;
int p = 0;
int i = 0;
if (nums != null && nums.length > 1) {
while (i < nums.length) {
if (i > 0 && nums[i] == nums[i - 1]) {
} else {
len++;
nums[p++] = nums[i];
}
i++;
}
} else if (nums.length == 1) {
len = 1;
}
return len;
}
public int removeDuplicates3(int[] nums) {
int i = 0;
for (int x : nums) {
if (i == 0 || x > nums[i - 1]) {
nums[i++] = x;
}
}
return i;
}
}