Skip to content

Commit b2f501c

Browse files
committed
add LeetCode No.26 删除排序数组中的重复项
1 parent a0dccd7 commit b2f501c

File tree

3 files changed

+164
-1
lines changed

3 files changed

+164
-1
lines changed

LeetCode/README.md

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,73 @@ https://leetcode.com/
88

99
## TODO
1010

11-
- [ ] 正式刷题
11+
- [x] 正式刷题(按tag来刷)
12+
13+
14+
### 数组
15+
16+
| # | title | 标题 | Code | Difficulty |
17+
| :--: | :--------------------------------------: | :--------------------------------------: | :--------------------------------------: | :--------: |
18+
| 26 | [Remove Duplicates from Sorted Array](https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/) | [删除排序数组中的重复项](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/description/) | [C++](code/0026_RemoveDuplicatesFromSortedArray.cpp) [Python](code/0026_RemoveDuplicatesFromSortedArray.py) | 简单(easy) |
19+
| | | | | |
20+
| | | | | |
21+
| | | | | |
22+
| | | | | |
23+
| | | | | |
24+
| | | | | |
25+
| | | | | |
26+
| | | | | |
27+
| | | | | |
28+
| | | | | |
29+
| | | | | |
30+
| | | | | |
31+
| | | | | |
32+
| | | | | |
33+
| | | | | |
34+
| | | | | |
35+
| | | | | |
36+
| | | | | |
37+
| | | | | |
38+
| | | | | |
39+
| | | | | |
40+
| | | | | |
41+
| | | | | |
42+
| | | | | |
43+
| | | | | |
44+
| | | | | |
45+
| | | | | |
46+
| | | | | |
47+
| | | | | |
48+
| | | | | |
49+
| | | | | |
50+
| | | | | |
51+
| | | | | |
52+
| | | | | |
53+
| | | | | |
54+
| | | | | |
55+
| | | | | |
56+
| | | | | |
57+
| | | | | |
58+
| | | | | |
59+
| | | | | |
60+
| | | | | |
61+
| | | | | |
62+
| | | | | |
63+
| | | | | |
64+
| | | | | |
65+
| | | | | |
66+
| | | | | |
67+
| | | | | |
68+
| | | | | |
69+
| | | | | |
70+
| | | | | |
71+
| | | | | |
72+
| | | | | |
73+
| | | | | |
74+
| | | | | |
75+
| | | | | |
76+
| | | | | |
77+
1278

1379

1480
## github
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*****************************************
2+
Copyright: Amusi
3+
Author: Amusi
4+
Date: 2018-07-29
5+
Reference: https://leetcode.com/problems/remove-duplicates-from-sorted-array/
6+
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array
7+
8+
题目描述
9+
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。
10+
11+
不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
12+
13+
示例 1:
14+
15+
给定数组 nums = [1,1,2],
16+
17+
函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。
18+
19+
你不需要考虑数组中超出新长度后面的元素。
20+
21+
示例 2:
22+
23+
给定 nums = [0,0,1,1,1,2,2,3,3,4],
24+
25+
函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。
26+
27+
你不需要考虑数组中超出新长度后面的元素。
28+
29+
*****************************************/
30+
31+
32+
class Solution {
33+
public:
34+
int removeDuplicates(vector<int>& nums) {
35+
int length = 0;
36+
if(nums.size() == 0)
37+
return length;
38+
length++;
39+
vector<int> indexs;
40+
for(int i=1; i<nums.size(); ++i){
41+
if(nums[i-1] != nums[i]){
42+
++length;
43+
nums[length-1] = nums[i];
44+
}
45+
}
46+
47+
return length;
48+
49+
}
50+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'''
2+
Copyright: Amusi
3+
Author: Amusi
4+
Date: 2018-07-29
5+
Reference: https://leetcode.com/problems/remove-duplicates-from-sorted-array/
6+
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array
7+
8+
题目描述
9+
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。
10+
11+
不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
12+
13+
示例 1:
14+
15+
给定数组 nums = [1,1,2],
16+
17+
函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。
18+
19+
你不需要考虑数组中超出新长度后面的元素。
20+
21+
示例 2:
22+
23+
给定 nums = [0,0,1,1,1,2,2,3,3,4],
24+
25+
函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。
26+
27+
你不需要考虑数组中超出新长度后面的元素。
28+
29+
'''
30+
31+
32+
class Solution:
33+
def removeDuplicates(self, nums):
34+
"""
35+
:type nums: List[int]
36+
:rtype: int
37+
"""
38+
length = 0
39+
if(len(nums)<=0):
40+
return length
41+
length+=1
42+
for i in range(1, len(nums)):
43+
if(nums[i-1]!=nums[i]):
44+
length+=1
45+
nums[length-1]=nums[i]
46+
return length
47+

0 commit comments

Comments
 (0)