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 16281aa

Browse files
增加题目59
1 parent 2f60a30 commit 16281aa

File tree

3 files changed

+186
-1
lines changed

3 files changed

+186
-1
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# 删除排序数组中的重复项 II
2+
3+
#### *Remove Duplicates from Sorted Array II*
4+
5+
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度。
6+
7+
不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
8+
9+
说明:
10+
11+
为什么返回数值是整数,但输出的答案是数组呢?
12+
13+
请注意,输入数组是以"引用"方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
14+
15+
你可以想象内部操作如下:
16+
17+
// nums 是以"引用"方式传递的。也就是说,不对实参做任何拷贝
18+
int len = removeDuplicates(nums);
19+
20+
// 在函数里修改输入数组对于调用者是可见的。
21+
// 根据你的函数返回的长度, 它会打印出数组中该长度范围内的所有元素。
22+
for (int i = 0; i < len; i++) {
23+
print(nums[i]);
24+
}
25+
26+
英文题目:
27+
28+
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.
29+
30+
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
31+
32+
**example 1**
33+
34+
```
35+
Given nums = [1,1,1,2,2,3],
36+
37+
Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively.
38+
39+
It doesn't matter what you leave beyond the returned length.
40+
41+
```
42+
43+
44+
**example 2**
45+
46+
```
47+
Given nums = [0,0,1,1,1,1,2,3,3],
48+
49+
Your function should return length = 7, with the first seven elements of nums being modified to 0, 0, 1, 1, 2, 3 and 3 respectively.
50+
51+
It doesn't matter what values are set beyond the returned length.
52+
53+
```
54+
55+
56+
---
57+
58+
### 思路
59+
60+
1. 遍历数组,index从零开始,使用count记录每个元素出现次数,大于2次则忽略,小于2次则赋值到index的位置,同时index++
61+
2. 遍历完毕返回index+1则为数组长度
62+
3. 同时附上评论区大神思路
63+
64+
65+
### 代码
66+
```
67+
68+
#include <vector>
69+
#include <iostream>
70+
using namespace std;
71+
72+
class Solution {
73+
public:
74+
int removeDuplicates(vector<int>& nums) {
75+
int count = 0;
76+
int index = 0;
77+
for (int i = 0; i < nums.size(); i++)
78+
{
79+
if (index < 1 || nums[i] == nums[index - 1] && count < 2)
80+
{
81+
nums[index++] = nums[i];
82+
++count;
83+
}
84+
else if (nums[i] != nums[index - 1])
85+
{
86+
nums[index++] = nums[i];
87+
count = 1;
88+
}
89+
}
90+
return index;
91+
}
92+
};
93+
94+
//大神思路
95+
int removeDuplicates(vector<int>& nums) {
96+
int i = 0;
97+
for (int n : nums)
98+
if (i < 2 || n > nums[i-2])
99+
nums[i++] = n;
100+
return i;
101+
}
102+
103+
```
104+
105+
本题以及其它leetcode题目代码github地址: [github地址](https:github.com/SherlockUnknowEn/leetcode)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/************************************************************************/
2+
/*
3+
Remove Duplicates from Sorted Array II
4+
5+
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.
6+
7+
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
8+
9+
Example 1:
10+
11+
Given nums = [1,1,1,2,2,3],
12+
13+
Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively.
14+
15+
It doesn't matter what you leave beyond the returned length.
16+
Example 2:
17+
18+
Given nums = [0,0,1,1,1,1,2,3,3],
19+
20+
Your function should return length = 7, with the first seven elements of nums being modified to 0, 0, 1, 1, 2, 3 and 3 respectively.
21+
22+
It doesn't matter what values are set beyond the returned length.
23+
Clarification:
24+
25+
Confused why the returned value is an integer but your answer is an array?
26+
27+
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
28+
29+
Internally you can think of this:
30+
31+
// nums is passed in by reference. (i.e., without making a copy)
32+
int len = removeDuplicates(nums);
33+
34+
// any modification to nums in your function would be known by the caller.
35+
// using the length returned by your function, it prints the first len elements.
36+
for (int i = 0; i < len; i++) {
37+
print(nums[i]);
38+
}
39+
40+
*/
41+
/************************************************************************/
42+
43+
#include <vector>
44+
#include <algorithm>
45+
#include <iostream>
46+
using namespace std;
47+
48+
class Solution {
49+
public:
50+
int removeDuplicates(vector<int>& nums) {
51+
int count = 0;
52+
int index = 0;
53+
for (int i = 0; i < nums.size(); i++)
54+
{
55+
if (index < 1 || nums[i] == nums[index - 1] && count < 2)
56+
{
57+
nums[index++] = nums[i];
58+
++count;
59+
}
60+
else if (nums[i] != nums[index - 1])
61+
{
62+
nums[index++] = nums[i];
63+
count = 1;
64+
}
65+
}
66+
return index;
67+
}
68+
};
69+
70+
int main()
71+
{
72+
vector<int> nums = { 1,1,1,2,2,3 };
73+
int len = Solution().removeDuplicates(nums);
74+
for (int i = 0; i < len; ++i)
75+
{
76+
cout << nums[i] << " ";
77+
}
78+
return 0;
79+
}

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,5 @@ note:
6161
##### 55. [Search a 2D Matrix](https://github.com/SherlockUnknowEn/leetcode/tree/master/50-59/55.%20Search%20a%202D%20Matrix(Medium)) 搜索二维矩阵
6262
##### 56. [Sort Colors](https://github.com/SherlockUnknowEn/leetcode/tree/master/50-59/56.%20Sort%20Colors(Medium)) 颜色排序
6363
##### 57. [Subsets](https://github.com/SherlockUnknowEn/leetcode/tree/master/50-59/57.%20Subsets(Medium)) 子集
64-
##### 58. [Word Search](https://github.com/SherlockUnknowEn/leetcode/tree/master/50-59/58.%20Word%20Search(Medium)) 单词搜索
64+
##### 58. [Word Search](https://github.com/SherlockUnknowEn/leetcode/tree/master/50-59/58.%20Word%20Search(Medium)) 单词搜索
65+
##### 59. [Remove Duplicates from Sorted Array II](https://github.com/SherlockUnknowEn/leetcode/tree/master/50-59/59.%20Remove%20Duplicates%20from%20Sorted%20Array%20II(Medium)) 删除排序数组中的重复项 II

0 commit comments

Comments
(0)

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