Remove Element

来源:互联网 发布:神仙劫坐骑升阶数据 编辑:程序博客网 时间:2024/05/16 05:39

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 cur_len = -1;        for(int i = 0; i < n; ++i)        {            if(A[i] != elem)             {                 cur_len++;                 if(cur_len != i)                 A[cur_len] = A[i];             }        }        return cur_len + 1;    }};


0 0
原创粉丝点击