1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Solution { public: void sortColors(vector<int>& nums) { int m = 0; int n = nums.size() - 1; for (int i = 0; i <= n; i++) { while (i <= n && nums[i] == 2) { swap(nums[i], nums[n]); --n; } if (nums[i] == 0) { swap(nums[i], nums[m]); ++m; } } } };
|