public void sortColors(int[] nums) { int left = 0; int right = nums.length-1; for (int i=0;i<=right;i++) { if (nums[i] == 0) { swap(i,left,nums); left++; } else if (nums[i] == 2){ swap(i,right,nums); i--; right--; } } } public void swap(int i, int j, int[] nums) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; }
原文:https://www.cnblogs.com/juniorMa/p/14547119.html