leetcode之27. Remove Element(C++解法)
2016-09-11 20:23
399 查看
题目:
Given an array and a value, remove all instances of that value in place and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
Example:
Given input array nums = [3,2,2,3], val = 3
Your function should return length = 2, with the first two elements of nums being 2.
[b]*********************[/b]我是分割线[b]*******************[/b]
class Solution
{
public:
int removeElement(vector& nums, int val)
{
int counts=0;
if(nums.empty())
return 0;
for(vector::iterator intel=(–nums.end()); intel>=nums.begin(); intel–)
{
if(*intel==val )
{
nums.erase(intel);
}
else
{
counts++;
}
}
return counts;
}
};
Given an array and a value, remove all instances of that value in place and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
Example:
Given input array nums = [3,2,2,3], val = 3
Your function should return length = 2, with the first two elements of nums being 2.
[b]*********************[/b]我是分割线[b]*******************[/b]
class Solution
{
public:
int removeElement(vector& nums, int val)
{
int counts=0;
if(nums.empty())
return 0;
for(vector::iterator intel=(–nums.end()); intel>=nums.begin(); intel–)
{
if(*intel==val )
{
nums.erase(intel);
}
else
{
counts++;
}
}
return counts;
}
};
相关文章推荐
- [C++]LeetCode 27: Remove Element(删除数组中指定元素)
- leetcode 27 Remove Element C++
- 【LeetCode】215. Kth Largest Element in an Array,基于Java和C++的解法
- Leetcode:Remove Element 菜鸟解法
- Leetcode 27. Remove Element (Easy) (java)
- Leetcode 27 Remove Element STL
- leetcode_c++:Remove Element (027)
- LeetCode 27:Remove Element
- LeetCode (27)Remove Element
- <LeetCode OJ>Remove Element【27】
- leetcode之26. Remove Duplicates from Sorted Array(C++解法)
- LeetCode 27. Remove Element(删除元素)
- leetCode 27.Remove Element (删除元素) 解题思路和方法
- [Leetcode]Leetcode 27:Remove Element
- leetCode 27.Remove Element (删除元素) 解题思路和方法
- leetcode之203. Remove Linked List Elements(C++解法)
- Leetcode:27. Remove Element(JAVA)
- 【LEETCODE】27-Remove Element
- leetcode Remove Element(27)
- leetcode上Remove Duplicates from Sorted Array的C++解法