LeetCode : Remove Duplicates from Sorted Array
2016-09-27 19:00
246 查看
常规方法是使用双指针。
只使用容器看起来比较简洁,但是效率会降低。
int removeDuplicates(vector<int>& nums) { if(nums.size() < 2) return nums.size(); int left = 1; while(left < nums.size()) { if(nums[left] == nums[left - 1]) break; left ++; } int right = left + 1; while(right < nums.size()) { if(nums[right] == nums[right - 1]) right ++; else { nums[left] = nums[right]; left ++; right ++; } } return left; }
只使用容器看起来比较简洁,但是效率会降低。
int removeDuplicates(vector<int>& nums) { set<int> s(nums.begin(), nums.end()); nums.assign(s.begin(), s.end()); return s.size(); }
相关文章推荐
- leetcode - 26 Remove Duplicates from Sorted Array
- 2017-09-12 LeetCode_026 Remove Duplicates from Sorted Array
- LeetCode 26 - Remove Duplicates from Sorted Array
- LeetCode 26.Remove Duplicates from Sorted Array
- Leetcode 26. Remove Duplicates from Sorted Array
- 【LeetCode】26 Remove Duplicates from Sorted Array
- Leetcode - Remove Duplicates from Sorted Array
- leetcode题解-26. Remove Duplicates from Sorted Array && 27. Remove Element
- [LeetCode] 26.Remove Duplicates from Sorted Array
- LeetCode-Remove Duplicates from Sorted Array
- leetcode挨个儿刷150514(9):Remove Duplicates from Sorted Array II
- [LeetCode]26. Remove Duplicates from Sorted Array
- LeetCode之Remove Duplicates from Sorted Array
- 【leetcode】Remove Duplicates from sorted array
- LeetCode: Remove Duplicates from Sorted Array
- leetcode_Remove Duplicates from Sorted Array
- ※ Leetcode - Array -Remove Duplicates from Sorted Array(就地有序数组去重)
- [leetcode]--26. Remove Duplicates from Sorted Array
- [Leetcode] Remove Duplicates from Sorted Array
- [LeetCode]Remove Duplicates from Sorted Array