share my simple C++ solution


  • 0
    F
    class Solution {
    public:
        void sortColors(vector<int>& nums) {
            int left=-1,right=nums.size();
            for(int i=0;i<right;i++){
                if(nums[i]==0) swap(nums[i],nums[++left]);
                if(nums[i]==2) swap(nums[i--],nums[--right]);
            }
        }
    };
    

Log in to reply
 

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