-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode0080.java
More file actions
36 lines (33 loc) · 1.07 KB
/
Copy pathLeetCode0080.java
File metadata and controls
36 lines (33 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
/* Remove Duplicates from Sorted Array II (See LeetCode0026)
* Example1:
* Given nums = [1,1,1,2,2,3],
* Your function should return length = 5,
* with the first five elements of nums being 1, 1, 2, 2 and 3 respectively.
* */
import java.util.Arrays;
public class LeetCode0080 {
public static void main(String[] args){
int[] nums = {0,0,1,1,1,1,2,3,3};
System.out.println(removeDuplicates(nums));
}
public static int removeDuplicates(int[] nums) {
int NonDupPoint = 1;
int Second = 0;
for(int i = 1; i < nums.length; i ++){
//首次重复
if(nums[i] == nums[i-1] && Second == 0){
nums[NonDupPoint] = nums[i];
NonDupPoint ++;
Second ++;
}
//不重复
if (nums[i] != nums[i-1]){
nums[NonDupPoint] = nums[i];
NonDupPoint ++;
Second = 0;
}
}
//System.out.println(Arrays.toString(nums));
return NonDupPoint;
}
}