https://leetcode-cn.com/problems/sort-colors/

荷兰国旗的划分

  1. public void sortColors(int[] nums) {
  2. int L = -1;
  3. int R = nums.length;
  4. int index = 0;
  5. while (index < R) {
  6. if (nums[index] == 0) {
  7. swap(nums, ++L, index++);
  8. } else if (nums[index] == 2){
  9. swap(nums, --R, index);
  10. } else { // 1
  11. index++;
  12. }
  13. }
  14. }
  15. public void swap(int[] arr, int i, int j) {
  16. int tmp = arr[i];
  17. arr[i] = arr[j];
  18. arr[j] = tmp;
  19. }