Sharing 3ms C++ solution


  • 0
    G
    class Solution {
    public:
        void sortColors(vector<int>& nums) {
            int lastPos = 0;
            int n = nums.size();
            int k = 0;
            
            while (k < 3) {
                for (int i = lastPos; i < n; i++) {
                    if (nums[i] == k) {
                        int temp = nums[i];
                        nums[i] = nums[lastPos];
                        nums[lastPos++] = temp;
                    }
                }
                k++;
            }
        }
    

    };


Log in to reply
 

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