【Leetcode】之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 len = nums.size();int cnt=1;int st=0;
if(len<2) return len;
for(int i=1;i<len;i++){
if(nums[i]!=nums[i-1]){
cnt++;
if(st<i-1){
nums[st+1]=nums[i];

}
st++;
}

}
return cnt;
}
};

Remove Duplicates from Sorted Array II -- LeetCode

2014-04-23 08:39:29

LeetCode 26 — Remove Duplicates from Sorted Array（C++ Java Python）

2014-02-23 15:12:20

Remove Duplicates from Sorted Array -- LeetCode

2014-02-27 05:12:22

LeetCode:Remove Duplicates from Sorted Array

2013-09-02 20:42:03

Leetcode_26_Remove Duplicates from Sorted Array

2014-11-27 23:21:40

[C++]LeetCode 26: Remove Duplicates from Sorted Array（有序数组去重）

2015-05-02 21:43:54

[leetcode-26]Remove Duplicates from Sorted Array(C)

2015-07-28 12:36:07

LeetCode-26 Remove Duplicates from Sorted Array(水题-去相同元素)

2015-04-29 17:29:39

LeetCode --- 26. Remove Duplicates from Sorted Array

2015-02-02 11:52:37

26. Remove Duplicates from Sorted Array (python)

2016-10-30 15:58:23