Remove Element

来源:互联网 发布:python递归算法 编辑:程序博客网 时间:2024/05/01 21:45

Remove Element

 Total Accepted: 8010 Total Submissions: 24406My Submissions

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) {        int i=0,j=0;        int len = n;        while (i<n) {            if (elem == A[i]){                ++i;                len--;            } else {                A[j++]=A[i++];            }        }        return len;    }};


0 0