Sort Colors 2021-06-29 19:34
public void sortColors(int[] nums) {
int length = nums.length;
int p0 = 0;
int p2 = length - 1;
for (int i = 0; i <= p2; i++) {
while (i <= p2 && nums[i] == 2) {
swap(nums, i, p2);
p2--;
}
if (nums[i] == 0) {
swap(nums, i, p0);
p0++;
}
}
}
private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
Runtime | Memory |
---|---|
0 ms | 37.7 MB |
EOF