Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
- Time: O(n)
- Space: O(1)
public void sortColors(int[] nums) {
int i = 0, l = 0, r = nums.length - 1;
while (i <= r) {
if (nums[i] == 0) {
nums[i++] = nums[l];
nums[l++] = 0;
} else if (nums[i] == 2) {
nums[i] = nums[r];
nums[r--] = 2;
} else {
i++;
}
}
}