Leetcode:Remove Element
2013-05-18 19:13
417 查看
Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
class Solution {
public:
int removeElement(int A[], int n, int elem) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int sumElem=0;
int num=n-1;
for( int i= 0; i <= num ; i ++ )
{
if(A[i]==elem)
{
sumElem++;
while( i < num && A[num] == elem)
{
sumElem++;
num--;
}
if(i==num)
{
return n-sumElem;
}
A[i] = A[num];
num--;
}
}
return n-sumElem;
}
};
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
class Solution {
public:
int removeElement(int A[], int n, int elem) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int sumElem=0;
int num=n-1;
for( int i= 0; i <= num ; i ++ )
{
if(A[i]==elem)
{
sumElem++;
while( i < num && A[num] == elem)
{
sumElem++;
num--;
}
if(i==num)
{
return n-sumElem;
}
A[i] = A[num];
num--;
}
}
return n-sumElem;
}
};
相关文章推荐
- LeetCode Remove Element
- Leetcode:remove_element
- LeetCode专题-Python实现之第27题:Remove Element
- leetcode 27. Remove Element移出元素(双指针)
- LeetCode 2 Remove Element
- 【LeetCode】2(7)Remove Element(Easy)
- (LeetCode)Remove Element --- 去掉指定元素
- LeetCode Remove Element
- leetcode Remove Element
- 【Leetcode】Remove Element in JAVA
- Leetcode: Remove Element
- Leetcode 27 Remove Element STL
- 【Leetcode】之Remove Element
- Remove Element -- LeetCode
- leetcode:数组之Remove Element
- LeetCode Remove Element
- [leetcode]Remove Element @ Python
- LeetCode : Remove Element [java]
- leetcode Remove Element
- LeetCode Remove Element