题目描述:
    image.png
    image.png
    解析:双指针 <1左边 =1中间 >1右边

    1. class Solution {
    2. public void sortColors(int[] nums) {
    3. int less = -1, more = nums.length, index = 0;
    4. while (index != more) { //循环终止条件
    5. if (nums[index] > 1) {
    6. swap(nums, index, --more);
    7. } else if (nums[index] < 1) {
    8. swap(nums, ++less, index++);
    9. } else {
    10. index++;
    11. }
    12. }
    13. }
    14. private void swap(int[] arr, int p1, int p2) {
    15. int tmp = arr[p1];
    16. arr[p1] = arr[p2];
    17. arr[p2] = tmp;
    18. }
    19. }