75. Sort Colors#
1public void sortColors(int[] nums) {
2 int lowEnd = 0, midEnd = 0;
3 int highStart = nums.length - 1;
4
5 while (midEnd <= highStart) {
6 if (nums[midEnd] == 0) {
7 swap(nums, midEnd++, lowEnd++);
8 } else if (nums[midEnd] == 1) {
9 midEnd++;
10 } else if (nums[midEnd] == 2) {
11 swap(nums, midEnd, highStart--);
12 }
13 }
14}
15
16private void swap(int[] arr, int a, int b) {
17 int tmp = arr[a];
18 arr[a] = arr[b];
19 arr[b] = tmp;
20}