26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int count = 0, size = nums.size();
if(size < 2)
return size;
int id = 1;
for(int i=1; i<size; ++i){
if(nums[i] != nums[i-1])
nums[id++] = nums[i];
}
return id;
}
};
相關文章
- Remove-duplicates-from-sorted-arrayREM
- leetcode Remove Duplicates from Sorted ArrayLeetCodeREM
- Leetcode 26 Remove Duplicates from Sorted ArrayLeetCodeREM
- Leetcode Remove Duplicates from Sorted Array IILeetCodeREM
- Leetcode-Remove Duplicates from Sorted ArrayLeetCodeREM
- Remove Duplicates from Sorted Array leetcode javaREMLeetCodeJava
- [leetcode]remove-duplicates-from-sorted-array-iiLeetCodeREM
- Leetcode-Remove Duplicates from Sorted Array IILeetCodeREM
- Remove Duplicates from Sorted Array II leetcode javaREMLeetCodeJava
- Remove Duplicates from Sorted ListREM
- 【leetcode】26. Remove Duplicates from Sorted Array 刪除有序陣列的重複元素LeetCodeREM陣列
- [LeetCode] 80. Remove Duplicates from Sorted Array IILeetCodeREM
- Remove-duplicates-from-sorted-listREM
- 83. Remove Duplicates from Sorted ListREM
- 【LeetCode從零單排】No26.Remove Duplicates from Sorted ArrayLeetCodeREM
- Leetcode Remove Duplicates from Sorted ListLeetCodeREM
- 82. Remove Duplicates from Sorted List IIREM
- Leetcode Remove Duplicates from Sorted List IILeetCodeREM
- Leetcode-Remove Duplicates from Sorted ListLeetCodeREM
- Remove Duplicates from Sorted List leetcode javaREMLeetCodeJava
- [LeetCode] Remove Duplicates from Sorted Array 有序陣列中去除重複項LeetCodeREM陣列
- LeetCode 83. Remove Duplicates from Sorted ListLeetCodeREM
- 【Leetcode】83. Remove Duplicates from Sorted ListLeetCodeREM
- Leetcode-Remove Duplicates from Sorted List IILeetCodeREM
- Remove Duplicates from Sorted List II leetcode javaREMLeetCodeJava
- 【Leetcode】82. Remove Duplicates from Sorted List IILeetCodeREM
- 【LeetCode從零單排】No83 Remove Duplicates from Sorted ListLeetCodeREM
- Remove Duplicates from Sorted List 去除連結串列中重複值節點REM
- LeetCode 83.Remove Duplicates from Sorted List(從已排序連結串列中除去重複) Easy/Linked ListLeetCodeREM排序
- [CareerCup] 2.1 Remove Duplicates from Unsorted List 移除無序連結串列中的重複項REM
- Leetcode 442. Find All Duplicates in an ArrayLeetCode
- Remove Untagged Images From DockerREMDocker
- Leetcode Search in Rotated Sorted ArrayLeetCode
- Leetcode Merge Sorted ArrayLeetCode
- Leetcode Remove Duplicates型別題目 (python)LeetCodeREM型別Python
- javascript Array.from()方法JavaScript
- Leetcode 33 Search in Rotated Sorted ArrayLeetCode
- Leetcode Search in Rotated Sorted Array IILeetCode