Skip to content

feat: add solution to leetcode No.0033 No.0081 #355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: add solutions to leetcode No.0026.Remove Duplicates from Sorted…
… Array
  • Loading branch information
gqjia committed Apr 6, 2021
commit a7a485db5a0747bff7d64c266a2caeaac26ae9f2
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

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

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

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

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

Expand Down Expand Up @@ -122,6 +122,28 @@ func removeDuplicates(nums []int) int {
}
```

### **C++**

```cpp
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int n = nums.size();
if(n < 2) {
return n;
}

int idx = 0;
for(int n : nums) {
if(idx < 1 || nums[idx-1] < n) {
nums[idx++] = n;
}
}
return idx;
}
};
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,28 @@ func removeDuplicates(nums []int) int {
}
```

### **C++**

```cpp
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int n = nums.size();
if(n < 2) {
return n;
}

int idx = 0;
for(int n : nums) {
if(idx < 1 || nums[idx-1] < n) {
nums[idx++] = n;
}
}
return idx;
}
};
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Author: Moriarty12138
*/
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int n = nums.size();
if(n < 2) {
return n;
}

int idx = 0;
for(int n : nums) {
if(idx < 1 || nums[idx-1] < n) {
nums[idx++] = n;
}
}
return idx;
}
};