三色问题

    科技2024-11-09  11

    class Solution { public: void sortColors(vector<int>& nums) { if (nums.empty()) { return; } int l = -1; int r = nums.size(); int idx = 0; while (idx < r) { if (nums[idx] == 0) { l++; swap(nums[idx], nums[l]); idx++; } else if (nums[idx] == 1) { idx++; } else { r--; swap(nums[idx], nums[r]); } } } };
    Processed: 0.014, SQL: 8