Skip to content

Commit

Permalink
add LeetCode No.26 删除排序数组中的重复项
Browse files Browse the repository at this point in the history
  • Loading branch information
amusi committed Jul 29, 2018
1 parent a0dccd7 commit b2f501c
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 1 deletion.
68 changes: 67 additions & 1 deletion LeetCode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,73 @@ https://leetcode.com/

## TODO

- [ ] 正式刷题
- [x] 正式刷题(按tag来刷)


### 数组

| # | title | 标题 | Code | Difficulty |
| :--: | :--------------------------------------: | :--------------------------------------: | :--------------------------------------: | :--------: |
| 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) |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |



## github
Expand Down
50 changes: 50 additions & 0 deletions LeetCode/code/0026_RemoveDuplicatesFromSortedArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*****************************************
Copyright: Amusi
Author: Amusi
Date: 2018-07-29
Reference: https://leetcode.com/problems/remove-duplicates-from-sorted-array/
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array
题目描述
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。
不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
示例 1:
给定数组 nums = [1,1,2],
函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。
你不需要考虑数组中超出新长度后面的元素。
示例 2:
给定 nums = [0,0,1,1,1,2,2,3,3,4],
函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。
你不需要考虑数组中超出新长度后面的元素。
*****************************************/


class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int length = 0;
if(nums.size() == 0)
return length;
length++;
vector<int> indexs;
for(int i=1; i<nums.size(); ++i){
if(nums[i-1] != nums[i]){
++length;
nums[length-1] = nums[i];
}
}

return length;

}
};
47 changes: 47 additions & 0 deletions LeetCode/code/0026_RemoveDuplicatesFromSortedArray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'''
Copyright: Amusi
Author: Amusi
Date: 2018-07-29
Reference: https://leetcode.com/problems/remove-duplicates-from-sorted-array/
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array
题目描述
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。
不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
示例 1:
给定数组 nums = [1,1,2],
函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。
你不需要考虑数组中超出新长度后面的元素。
示例 2:
给定 nums = [0,0,1,1,1,2,2,3,3,4],
函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。
你不需要考虑数组中超出新长度后面的元素。
'''


class Solution:
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
length = 0
if(len(nums)<=0):
return length
length+=1
for i in range(1, len(nums)):
if(nums[i-1]!=nums[i]):
length+=1
nums[length-1]=nums[i]
return length

0 comments on commit b2f501c

Please sign in to comment.