File tree 7 files changed +156
-50
lines changed
solution/0000-0099/0080.Remove Duplicates from Sorted Array II
7 files changed +156
-50
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -47,22 +47,87 @@ for (int i = 0; i < len; i++) {
47
47
48
48
<!-- 这里可写通用的实现逻辑 -->
49
49
50
+ 从数组下标 1 开始遍历数组。
51
+
52
+ 用计数器 ` cnt ` 记录当前数字重复出现的次数,` cnt ` 的最小计数为 0;用 ` cur ` 记录新数组下个待覆盖的元素位置。
53
+
54
+ 遍历时,若当前元素 ` nums[i] ` 与上个元素 ` nums[i-1] ` 相同,则计数器 +1,否则计数器重置为 0。如果计数器小于 2,说明当前元素 ` nums[i] ` 可以添加到新数组中,即:` nums[cur] = nums[i] ` ,同时 ` cur++ ` 。
55
+
56
+ 遍历结果,返回 ` cur ` 值即可。
57
+
50
58
<!-- tabs:start -->
51
59
52
60
### ** Python3**
53
61
54
62
<!-- 这里可写当前语言的特殊实现逻辑 -->
55
63
56
64
``` python
57
-
65
+ class Solution :
66
+ def removeDuplicates (self , nums : List[int ]) -> int :
67
+ n = len (nums)
68
+ cnt, cur = 0 , 1
69
+ for i in range (1 , n):
70
+ if nums[i] == nums[i - 1 ]:
71
+ cnt += 1
72
+ else :
73
+ cnt = 0
74
+ if cnt < 2 :
75
+ nums[cur] = nums[i]
76
+ cur += 1
77
+ return cur
58
78
```
59
79
60
80
### ** Java**
61
81
62
82
<!-- 这里可写当前语言的特殊实现逻辑 -->
63
83
64
84
``` java
85
+ class Solution {
86
+ public int removeDuplicates (int [] nums ) {
87
+ int cnt = 0 , cur = 1 ;
88
+ for (int i = 1 ; i < nums. length; ++ i) {
89
+ if (nums[i] == nums[i - 1 ]) ++ cnt;
90
+ else cnt = 0 ;
91
+ if (cnt < 2 ) nums[cur++ ] = nums[i];
92
+ }
93
+ return cur;
94
+ }
95
+ }
96
+ ```
97
+
98
+ ### ** C++**
99
+
100
+ ``` cpp
101
+ class Solution {
102
+ public:
103
+ int removeDuplicates(vector<int >& nums) {
104
+ int n = nums.size();
105
+ int cnt = 0, cur = 1;
106
+ for (int i = 1; i < n; ++i) {
107
+ if (nums[ i] == nums[ i - 1] ) ++cnt;
108
+ else cnt = 0;
109
+ if (cnt < 2) nums[ cur++] = nums[ i] ;
110
+ }
111
+ return cur;
112
+ }
113
+ };
114
+ ```
65
115
116
+ ### **C#**
117
+
118
+ ```cs
119
+ public class Solution {
120
+ public int RemoveDuplicates(int[] nums) {
121
+ int cnt = 0, cur = 1;
122
+ for (int i = 1; i < nums.Length; ++i)
123
+ {
124
+ if (nums[i] == nums[i - 1]) ++cnt;
125
+ else cnt = 0;
126
+ if (cnt < 2) nums[cur++] = nums[i];
127
+ }
128
+ return cur;
129
+ }
130
+ }
66
131
```
67
132
68
133
### ** ...**
Original file line number Diff line number Diff line change @@ -73,19 +73,76 @@ for (int i = 0; i < len; i++) {
73
73
### ** Python3**
74
74
75
75
``` python
76
-
76
+ class Solution :
77
+ def removeDuplicates (self , nums : List[int ]) -> int :
78
+ n = len (nums)
79
+ cnt, cur = 0 , 1
80
+ for i in range (1 , n):
81
+ if nums[i] == nums[i - 1 ]:
82
+ cnt += 1
83
+ else :
84
+ cnt = 0
85
+ if cnt < 2 :
86
+ nums[cur] = nums[i]
87
+ cur += 1
88
+ return cur
77
89
```
78
90
79
91
### ** Java**
80
92
81
93
``` java
94
+ class Solution {
95
+ public int removeDuplicates (int [] nums ) {
96
+ int cnt = 0 , cur = 1 ;
97
+ for (int i = 1 ; i < nums. length; ++ i) {
98
+ if (nums[i] == nums[i - 1 ]) ++ cnt;
99
+ else cnt = 0 ;
100
+ if (cnt < 2 ) nums[cur++ ] = nums[i];
101
+ }
102
+ return cur;
103
+ }
104
+ }
105
+ ```
82
106
107
+ ### ** C++**
108
+
109
+ ``` cpp
110
+ class Solution {
111
+ public:
112
+ int removeDuplicates(vector<int >& nums) {
113
+ int n = nums.size();
114
+ int cnt = 0, cur = 1;
115
+ for (int i = 1; i < n; ++i) {
116
+ if (nums[ i] == nums[ i - 1] ) ++cnt;
117
+ else cnt = 0;
118
+ if (cnt < 2) nums[ cur++] = nums[ i] ;
119
+ }
120
+ return cur;
121
+ }
122
+ };
83
123
```
84
124
125
+ ### **C#**
126
+
127
+ ```cs
128
+ public class Solution {
129
+ public int RemoveDuplicates(int[] nums) {
130
+ int cnt = 0, cur = 1;
131
+ for (int i = 1; i < nums.Length; ++i)
132
+ {
133
+ if (nums[i] == nums[i - 1]) ++cnt;
134
+ else cnt = 0;
135
+ if (cnt < 2) nums[cur++] = nums[i];
136
+ }
137
+ return cur;
138
+ }
139
+ }
140
+
85
141
### **...**
86
142
87
143
```
88
144
89
145
```
90
146
91
147
<!-- tabs:end -->
148
+ ```
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public:
3
3
int removeDuplicates (vector<int >& nums) {
4
- if (nums.empty ())return 0 ;
5
- size_t len = nums.size ();
6
- if (len == 1 )return 1 ;
7
-
8
- auto iter = nums.begin ();
9
- iter++;
10
- int k = 1 ;
11
- while (iter != nums.end ()){
12
- if (*iter == *(iter-1 ))k++;
13
- else k = 1 ;
14
-
15
- if (k==3 ){
16
- nums.erase (iter);
17
- k--;
18
- }
19
- else {
20
- iter++;
21
- }
4
+ int n = nums.size ();
5
+ int cnt = 0 , cur = 1 ;
6
+ for (int i = 1 ; i < n; ++i) {
7
+ if (nums[i] == nums[i - 1 ]) ++cnt;
8
+ else cnt = 0 ;
9
+ if (cnt < 2 ) nums[cur++] = nums[i];
22
10
}
23
-
24
- len = nums.size ();
25
- return len;
11
+ return cur;
26
12
}
27
13
};
Original file line number Diff line number Diff line change 1
1
public class Solution {
2
2
public int RemoveDuplicates ( int [ ] nums ) {
3
- if ( nums . Length <= 2 ) return nums . Length ;
4
- var i = 0 ;
5
- var j = 1 ;
6
- while ( j < nums . Length )
3
+ int cnt = 0 , cur = 1 ;
4
+ for ( int i = 1 ; i < nums . Length ; ++ i )
7
5
{
8
- if ( nums [ i ] != nums [ j ] || i == 0 || nums [ i ] == nums [ j ] && nums [ i - 1 ] != nums [ j ] )
9
- {
10
- nums [ ++ i ] = nums [ j ] ;
11
- }
12
- ++ j ;
6
+ if ( nums [ i ] == nums [ i - 1 ] ) ++ cnt ;
7
+ else cnt = 0 ;
8
+ if ( cnt < 2 ) nums [ cur ++ ] = nums [ i ] ;
13
9
}
14
- return i + 1 ;
10
+ return cur ;
15
11
}
16
12
}
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int removeDuplicates (int [] nums ) {
3
- if (nums .length <3 ) return nums .length ;
4
- int pos = 1 ,flag = 1 ,last = nums [0 ];
5
- for (int i = 1 ;i <nums .length ;i ++){
6
- if (nums [i ] == last ) flag ++;
7
- else {
8
- flag = 1 ;
9
- last = nums [i ];
10
- }
11
- if (flag <= 2 ) nums [pos ++] = last ;
3
+ int cnt = 0 , cur = 1 ;
4
+ for (int i = 1 ; i < nums .length ; ++i ) {
5
+ if (nums [i ] == nums [i - 1 ]) ++cnt ;
6
+ else cnt = 0 ;
7
+ if (cnt < 2 ) nums [cur ++] = nums [i ];
12
8
}
13
- return pos ;
9
+ return cur ;
14
10
}
15
11
}
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def removeDuplicates (self , nums : List [int ]) -> int :
3
+ n = len (nums )
4
+ cnt , cur = 0 , 1
5
+ for i in range (1 , n ):
6
+ if nums [i ] == nums [i - 1 ]:
7
+ cnt += 1
8
+ else :
9
+ cnt = 0
10
+ if cnt < 2 :
11
+ nums [cur ] = nums [i ]
12
+ cur += 1
13
+ return cur
You can’t perform that action at this time.
0 commit comments