LeetCode刷题(C++)——Remove Duplicates from Sorted Array(Easy)
2017-05-07 14:21
337 查看
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 =
Your function should return length =
It doesn't matter what you leave beyond the new length.
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.size() < 2)
return nums.size();
for (vector<int>::iterator it = nums.begin()+1;it !=nums.end();)
{
if (*it == *(it - 1))
it = nums.erase(it);
else
it++;
}
return nums.size();
}
};
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
1and
2respectively.
It doesn't matter what you leave beyond the new length.
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.size() < 2)
return nums.size();
for (vector<int>::iterator it = nums.begin()+1;it !=nums.end();)
{
if (*it == *(it - 1))
it = nums.erase(it);
else
it++;
}
return nums.size();
}
};
相关文章推荐
- [Leetcode 26, easy] Remove Duplicates from Sorted Array
- 【leetcode c++】26 Remove Duplicates from Sorted Array
- LeetCode刷题(C++)——Remove Duplicates from Sorted Array II(Medium)
- leetcode上Remove Duplicates from Sorted Array的C++解法
- [C++]LeetCode 26: Remove Duplicates from Sorted Array(有序数组去重)
- LeetCode-Easy刷题(7) Remove Duplicates from Sorted Array
- LeetCode_Easy心得:26. Remove Duplicates from Sorted Array(C语言)
- leetcode之26. Remove Duplicates from Sorted Array(C++解法)
- 【Leetcode-easy】Remove Duplicates from Sorted Array
- LeetCode-26-Remove Duplicates from Sorted Array(排序/内部排序)-Easy
- [C++]LeetCode: 2 Remove Duplicates from Sorted Array
- leetcode刷题系列C++-Remove Duplicates From Sorted Array II
- [array] leetCode-26. Remove Duplicates from Sorted Array - Easy
- [C++]LeetCode: 72 Remove Duplicates from Sorted Array II
- leetcode_26_ Remove Duplicates from Sorted Array (easy)
- 【leetcode c++】80 Remove Duplicates from Sorted Array II
- LeetCode刷题(C++)——Remove Duplicates from Sorted List(Easy)
- leetcode 26[easy]--- Remove Duplicates from Sorted Array
- 【C++】【LeetCode】26. Remove Duplicates from Sorted Array
- Leetcode 26. Remove Duplicates from Sorted Array (Easy) (cpp)