File tree 4 files changed +68
-41
lines changed
solution/0000-0099/0026.Remove Duplicates from Sorted Array
4 files changed +68
-41
lines changed Original file line number Diff line number Diff line change @@ -128,20 +128,37 @@ func removeDuplicates(nums []int) int {
128
128
class Solution {
129
129
public:
130
130
int removeDuplicates(vector<int >& nums) {
131
- int n = nums.size();
132
- if(n < 2) {
133
- return n;
131
+ int cnt = 0, n = nums.size();
132
+ for (int i = 1; i < n; ++i) {
133
+ if (nums[ i] == nums[ i - 1] ) ++cnt;
134
+ else nums[ i - cnt] = nums[ i] ;
134
135
}
136
+ return n - cnt;
137
+ }
138
+ };
139
+ ```
135
140
136
- int idx = 0;
137
- for(int n : nums) {
138
- if(idx < 1 || nums[idx-1] < n) {
139
- nums[idx++] = n;
141
+ ### **C#**
142
+
143
+ ```cs
144
+ public class Solution {
145
+ public int RemoveDuplicates(int[] nums) {
146
+ int cnt = 0;
147
+ int n = nums.Length;
148
+ for (int i = 1; i < n; ++i)
149
+ {
150
+ if (nums[i] == nums[i - 1])
151
+ {
152
+ ++cnt;
153
+ }
154
+ else
155
+ {
156
+ nums[i - cnt] = nums[i];
140
157
}
141
158
}
142
- return idx ;
159
+ return n - cnt ;
143
160
}
144
- };
161
+ }
145
162
```
146
163
147
164
### ** ...**
Original file line number Diff line number Diff line change @@ -138,20 +138,37 @@ func removeDuplicates(nums []int) int {
138
138
class Solution {
139
139
public:
140
140
int removeDuplicates(vector<int >& nums) {
141
- int n = nums.size();
142
- if(n < 2) {
143
- return n;
141
+ int cnt = 0, n = nums.size();
142
+ for (int i = 1; i < n; ++i) {
143
+ if (nums[ i] == nums[ i - 1] ) ++cnt;
144
+ else nums[ i - cnt] = nums[ i] ;
144
145
}
146
+ return n - cnt;
147
+ }
148
+ };
149
+ ```
145
150
146
- int idx = 0;
147
- for(int n : nums) {
148
- if(idx < 1 || nums[idx-1] < n) {
149
- nums[idx++] = n;
151
+ ### **C#**
152
+
153
+ ```cs
154
+ public class Solution {
155
+ public int RemoveDuplicates(int[] nums) {
156
+ int cnt = 0;
157
+ int n = nums.Length;
158
+ for (int i = 1; i < n; ++i)
159
+ {
160
+ if (nums[i] == nums[i - 1])
161
+ {
162
+ ++cnt;
163
+ }
164
+ else
165
+ {
166
+ nums[i - cnt] = nums[i];
150
167
}
151
168
}
152
- return idx ;
169
+ return n - cnt ;
153
170
}
154
- };
171
+ }
155
172
```
156
173
157
174
### ** ...**
Original file line number Diff line number Diff line change 1
- /* *
2
- * Author: Moriarty12138
3
- */
4
1
class Solution {
5
2
public:
6
3
int removeDuplicates (vector<int >& nums) {
7
- int n = nums.size ();
8
- if (n < 2 ) {
9
- return n;
4
+ int cnt = 0 , n = nums.size ();
5
+ for (int i = 1 ; i < n; ++i) {
6
+ if (nums[i] == nums[i - 1 ]) ++cnt;
7
+ else nums[i - cnt] = nums[i];
10
8
}
11
-
12
- int idx = 0 ;
13
- for (int n : nums) {
14
- if (idx < 1 || nums[idx-1 ] < n) {
15
- nums[idx++] = n;
16
- }
17
- }
18
- return idx;
9
+ return n - cnt;
19
10
}
20
- };
11
+ };
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 ;
4
+ int n = nums . Length ;
5
+ for ( int i = 1 ; i < n ; ++ i )
7
6
{
8
- if ( nums [ i ] != nums [ j ] )
7
+ if ( nums [ i ] == nums [ i - 1 ] )
9
8
{
10
- nums [ ++ i ] = nums [ j ] ;
9
+ ++ cnt ;
10
+ }
11
+ else
12
+ {
13
+ nums [ i - cnt ] = nums [ i ] ;
11
14
}
12
- ++ j ;
13
15
}
14
- return i + 1 ;
16
+ return n - cnt ;
15
17
}
16
18
}
You can’t perform that action at this time.
0 commit comments