-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemove.java
More file actions
62 lines (57 loc) · 1.61 KB
/
Remove.java
File metadata and controls
62 lines (57 loc) · 1.61 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package removeDuplicates;
import java.util.ArrayList;
import java.util.List;
public class Remove {
public int removeDuplicates(int[] nums) {
if (nums.length <= 2){
return nums.length;
}
List<Integer> res = new ArrayList<>();
int tmp = nums[0], count = 1;
res.add(nums[0]);
for (int i = 1; i < nums.length; i++){
if (nums[i] == tmp){
if (count < 2){
res.add(nums[i]);
count++;
}
}
else {
tmp = nums[i];
count = 1;
res.add(nums[i]);
}
}
for (int i = 0; i< res.size(); i++){
nums[i] = res.get(i);
}
return res.size();
}
public int removeDuplicates2(int[] nums){
if (nums == null || nums.length == 0)
return 0;
int n = nums.length, flag = 1;
int pos = 0;
for (int i = 1; i < n; ++i) {
if (nums[i] != nums[pos]) {
++pos;
nums[pos] = nums[i];
flag = 1;
}
else {
if (flag == 1) {
flag = 2;
++pos;
nums[pos] = nums[i];
}
}
}
return pos + 1;
}
public static void main(String[] args){
int[] nums = new int[]{1,1,1,2,2,2};
Remove t = new Remove();
int length = t.removeDuplicates2(nums);
System.out.println(length);
}
}