Short 4ms in-place linear time C++ solution


  • 0
    D
    class Solution {
    public:
        void sortColors(vector<int>& nums) {
            int i = 0, j = 0, k = nums.size() - 1;
            while (j <= k) {
                if (nums[j] == 0) {
                    std::swap(nums[i++], nums[j++]);
                } else if (nums[j] == 1) {
                    j++;
                } else {
                    std::swap(nums[j], nums[k--]);
                }
            }
        }
    };

Log in to reply
 

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