Skip to content

Commit e2b096c

Browse files
gqjiayanglbme
authored andcommitted
feat: add solutions to leetcode No.0026.Remove Duplicates from Sorted Array
1 parent ba06ec7 commit e2b096c

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

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

+24-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
<p><strong>示例&nbsp;1:</strong></p>
1515

16-
<pre>给定数组 <em>nums</em> = <strong>[1,1,2]</strong>,
16+
<pre>给定数组 <em>nums</em> = <strong>[1,1,2]</strong>,
1717

18-
函数应该返回新的长度 <strong>2</strong>, 并且原数组 <em>nums </em>的前两个元素被修改为 <strong><code>1</code></strong>, <strong><code>2</code></strong>。
18+
函数应该返回新的长度 <strong>2</strong>, 并且原数组 <em>nums </em>的前两个元素被修改为 <strong><code>1</code></strong>, <strong><code>2</code></strong>。
1919

2020
你不需要考虑数组中超出新长度后面的元素。</pre>
2121

@@ -122,6 +122,28 @@ func removeDuplicates(nums []int) int {
122122
}
123123
```
124124

125+
### **C++**
126+
127+
```cpp
128+
class Solution {
129+
public:
130+
int removeDuplicates(vector<int>& nums) {
131+
int n = nums.size();
132+
if(n < 2) {
133+
return n;
134+
}
135+
136+
int idx = 0;
137+
for(int n : nums) {
138+
if(idx < 1 || nums[idx-1] < n) {
139+
nums[idx++] = n;
140+
}
141+
}
142+
return idx;
143+
}
144+
};
145+
```
146+
125147
### **...**
126148

127149
```

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

+22
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,28 @@ func removeDuplicates(nums []int) int {
132132
}
133133
```
134134

135+
### **C++**
136+
137+
```cpp
138+
class Solution {
139+
public:
140+
int removeDuplicates(vector<int>& nums) {
141+
int n = nums.size();
142+
if(n < 2) {
143+
return n;
144+
}
145+
146+
int idx = 0;
147+
for(int n : nums) {
148+
if(idx < 1 || nums[idx-1] < n) {
149+
nums[idx++] = n;
150+
}
151+
}
152+
return idx;
153+
}
154+
};
155+
```
156+
135157
### **...**
136158

137159
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Author: Moriarty12138
3+
*/
4+
class Solution {
5+
public:
6+
int removeDuplicates(vector<int>& nums) {
7+
int n = nums.size();
8+
if(n < 2) {
9+
return n;
10+
}
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;
19+
}
20+
};

0 commit comments

Comments
 (0)