My C++ solution. Runtime 3ms.


  • 0
    S

    My C++ solution.
    Runtime 3ms.

    class Solution
    {
    public:
        int removeElement(vector<int>& nums, int val)
        {
            if(nums.size()==0)return 0;
            int low=0,high=nums.size()-1;
            while(low<high)
            {
                while(low<high && nums[low]!=val)
                    low++;
                while(low<high && nums[high]==val)
                    high--;
                swap(nums[low],nums[high]);
            }
            if(nums[low]==val) return low;
            else return low+1;
        }
    };

Log in to reply
 

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