Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 534f729

Browse files
committed
feat: update leetcode solutions: No.0026. Remove Duplicates from Sorted Array
1 parent 2e1ab81 commit 534f729

File tree

4 files changed

+68
-41
lines changed

4 files changed

+68
-41
lines changed

‎solution/0000-0099/0026.Remove Duplicates from Sorted Array/README.md‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,20 +128,37 @@ func removeDuplicates(nums []int) int {
128128
class Solution {
129129
public:
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
### **...**

‎solution/0000-0099/0026.Remove Duplicates from Sorted Array/README_EN.md‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,20 +138,37 @@ func removeDuplicates(nums []int) int {
138138
class Solution {
139139
public:
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
### **...**
Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
1-
/**
2-
* Author: Moriarty12138
3-
*/
41
class Solution {
52
public:
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+
};
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
public 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
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /