forked from algorithm020/algorithm020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveDuplicates.java
More file actions
50 lines (45 loc) · 1.07 KB
/
RemoveDuplicates.java
File metadata and controls
50 lines (45 loc) · 1.07 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
import java.util.ArrayList;
import java.util.List;
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
/**
* 双指针的方式.
*
* @param nums
* @return
*/
public int removeDuplicates(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int i = 0;
for (int j = 1; j < nums.length; j++) {
if (nums[i] != nums[j]) {
i++;
nums[i] = nums[j];
}
}
return i + 1;
}
/**
* list 存储不重复的数据.
*
* @param nums
* @return
*/
public int removeDuplicates(int[] nums) {
List<Integer> list = new ArrayList<>();
for(int num : nums){
if(list.contains(num)){
continue;
}
list.add(num);
}
int i = 0;
for (Integer integer : list) {
nums[i++] = integer;
}
return list.size();
}
}
//leetcode submit region end(Prohibit modification and deletion)