public class Solution {
    public void sortColors(int[] nums) {
      // since we only have 3 buckets, we put each colour into
      // its own bucket and count
      // then we overwrite original array from those buckets
      int[] buckets = new int[3];
      for (int elem : nums) {
        buckets[elem]++;
      }

      int index = 0;
      for (int i = 0; i < buckets.length; i++) {
        while (buckets[i] > 0) {
          nums[index] = i;
          index++;
          buckets[i]--;
        }
      }
    }

  // This solution is taken from leetcode forum
  // single pass and constant space (two pointers)
  public void sortColors2(int[] nums) {
    int zero = 0;
    int two = nums.length - 1;
    for (int i = zero; i <= two; i++) {
      while (nums[i] == 2 && i < two) {
        swap(nums, i, two);
        two--;
      }
      while (nums[i] == 0 && i > zero) {
        swap(nums, i, zero);
        zero++;
      }
    }
  }

  private void swap(int[] arr, int i, int j) {
    arr[i] = arr[i] ^ arr[j];
    arr[j] = arr[i] ^ arr[j];
    arr[i] = arr[i] ^ arr[j];
  }

  // My single-pass and constant space solution
  public void sortColors3(int[] nums) {
    int i = 0;
    int j = nums.length - 1;
    int bucket = 0;
    while (i < j && bucket < 2) {
      while (i < j && nums[i] == bucket) i++;
      while (i < j && nums[j] != bucket) j--;
      if (i < j && nums[i] > nums[j]) {
        nums[i] = nums[i] ^ nums[j];
        nums[j] = nums[i] ^ nums[j];
        nums[i] = nums[i] ^ nums[j];
      } else {
        j = nums.length - 1;
        bucket++;
      }
    }
  }
}

@@ -1586,11 +1586,506 @@
    %7D%0A  %7D
+%0A%0A  // My another single-pass and constant space solution,%0A  // slightly simplified version above%0A  public void sortColors(int%5B%5D nums) %7B%0A    int i = 0, j = nums.length - 1, colour = 0;%0A    while (i %3C j && colour %3C 2) %7B%0A      while (i %3C nums.length && nums%5Bi%5D %3C= colour) i++;%0A      while (j %3E 0 && nums%5Bj%5D %3E colour) j--;%0A      if (i %3C j) %7B%0A        int tmp = nums%5Bi%5D;%0A        nums%5Bi%5D = nums%5Bj%5D;%0A        nums%5Bj%5D = tmp;%0A      %7D else %7B%0A        colour++;%0A        j = nums.length - 1;%0A      %7D%0A    %7D%0A  %7D
 %0A%7D%0A

update solution