阅读量:0
1、颜色分类 - 力扣(LeetCode)
思路:
- 创建三个指针,然后把数组分为三个区域
- 遍历
- 代码:
class Solution { public void swap(int[] nums, int i, int j){ int t = nums[i]; nums[i] = nums[j]; nums[j] = t; } public void sortColors(int[] nums) { int left = -1; int right = nums.length; int i = 0; while(i < right){ if(nums[i] == 0){ swap(nums, ++left, i++); }else if(nums[i] == 1){ i++; }else{ swap(nums, --right, i); } } } }