LeetCode27——Remove Element

来源:互联网 发布:自己做优惠券淘宝客 编辑:程序博客网 时间:2024/06/03 04:02

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.

难度系数:

容易

实现

int removeElement(int A[], int n, int elem) {    int cnt = 0;    for (int i = 0; i < n - cnt; ++i) {        if (elem == A[i]) {            A[i] = A[n- 1 - cnt];            cnt++;            i--;        }    }    return n - cnt;}
0 0
原创粉丝点击