import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param colors int整型一维数组
     * @return int整型一维数组
     */
    public int[] sortColor (int[] colors) {
        // write code here
        int i = 0; // 扫描指针,也就是赋值指针
        int l = 0; // 左指针
        int r = colors.length - 1; // 右指针
        while (i <= r) { // 从头到尾扫描
            if (colors[i] == 0) {  // 等于0的与左指针交换,然后赋值指针++,左指针++
                swap(colors, i++, l++);
            } else if (colors[i] == 1) { // 等于1的跳过不做处理
                i++;
            } else { // 来到这个的2的需要与右边指针交换,同时右指针左移
                swap(colors, i, r--);
            }
        }
        return colors;
    }

    private void swap(int[] colors, int i, int j) {
        int tmp = colors[i];
        colors[i] = colors[j];
        colors[j] = tmp;
    }
}

解题思想:三指针:左右指针+扫描赋值指针,总共三个数,0肯定在左侧,2肯定在右侧,中间的肯定是0.