File tree Expand file tree Collapse file tree 4 files changed +68
-41
lines changed
solution/0000-0099/0026.Remove Duplicates from Sorted Array Expand file tree Collapse file tree 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 {
128128class Solution {
129129public:
130130 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] ;
134135 }
136+ return n - cnt;
137+ }
138+ };
139+ ```
135140
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];
140157 }
141158 }
142- return idx ;
159+ return n - cnt ;
143160 }
144- };
161+ }
145162```
146163
147164### ** ...**
Original file line number Diff line number Diff line change @@ -138,20 +138,37 @@ func removeDuplicates(nums []int) int {
138138class Solution {
139139public:
140140 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] ;
144145 }
146+ return n - cnt;
147+ }
148+ };
149+ ```
145150
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];
150167 }
151168 }
152- return idx ;
169+ return n - cnt ;
153170 }
154- };
171+ }
155172```
156173
157174### ** ...**
Original file line number Diff line number Diff line change 1- /* *
2- * Author: Moriarty12138
3- */
41class Solution {
52public:
63 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];
108 }
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;
1910 }
20- };
11+ };
Original file line number Diff line number Diff line change 11public class Solution {
22 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 )
76 {
8- if ( nums [ i ] != nums [ j ] )
7+ if ( nums [ i ] == nums [ i - 1 ] )
98 {
10- nums [ ++ i ] = nums [ j ] ;
9+ ++ cnt ;
10+ }
11+ else
12+ {
13+ nums [ i - cnt ] = nums [ i ] ;
1114 }
12- ++ j ;
1315 }
14- return i + 1 ;
16+ return n - cnt ;
1517 }
1618}
You can’t perform that action at this time.
0 commit comments