problem:
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.
Array Two Pointers
thinking:
(1)将一个数组的指定元素删除,没有排序要求,这个题目很简单,只需将elem的位置用后面的元素替换掉即可
(2)边界条件:n为0时
code:
class Solution { public: int removeElement(int A[], int n, int elem) { int count=0; if(n==0) return n; for(int i=0;i<n;i++) { if(A[i]!=elem) { A[count]=A[i]; count++; } } return count; } };
leetcode 题解 || Remove Element 问题
原文:http://blog.csdn.net/hustyangju/article/details/44587007