LeetCode Remove Element

class Solution {

public:

    int removeElement(int A[], int n, int elem) {

        if (A == NULL || n < 1) return 0;

        int rpos = 0, wpos = 0;

        int removed = 0;

        while (rpos < n) {

            if (A[rpos] == elem) {

                rpos++;

                removed++;

            } else {

                A[wpos++] = A[rpos++];

            }

        }

        return n - removed;

    }

};

再水

你可能感兴趣的:(LeetCode)