Leetcode:Remove Element
2013-05-18 17:54
337 查看
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 27. Remove Element
- LeetCode_Remove Element
- leetcode 27 -- Remove Element
- 【leetcode】Remove Element
- LeetCode-27-Remove Element(数组)-Easy
- [leetcode]Remove Element
- [leetcode 27] Remove Element
- leetcode 27: Remove Element
- Leetcode: 27. Remove Element
- [LeetCode] Remove Element
- [Leetcode] Remove Element
- leetcode 027 Remove Element(难易度:Easy)
- 开始刷leetcode day10:Remove Element
- [Leetcode][python]Remove Element
- Leetcode: Remove Element
- 【leetcode】Remove Element
- leetcode: Remove Element
- [LeetCode]Remove Element
- LeetCode 27. Remove Element