A neat c++ solution


  • 0
    C
    class Solution {
    public:
        int removeElement(vector<int>& nums, int val) {
            for (auto it(nums.rbegin()); it != nums.rend(); ++it) {
                if (*it == val) {
                    swap(*it, *nums.rbegin());
                    nums.erase(nums.end() - 1);
                }
            }
            return nums.size();
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.