标签:
题目描述public class Solution { public void SortColors(int[] nums) { FindAndSwap(nums, 0, 2); FindAndSwap(nums, 0, 1); FindAndSwap(nums, 1, 2); } public void FindAndSwap(int[] nums, int left, int right){ var i = 0; var k = nums.Length - 1; while(i < k){ while(nums[i] != right && i < k){ i++; } while(nums[k] != left && k > 0){ k--; } if(i < k){ var t = nums[i]; nums[i] = nums[k]; nums[k] = t; } } } }
public class Solution { public void SortColors(int[] nums) { var i = 0; var k = nums.Length - 1; var j = 0; while(j <= k){ if(nums[j] == 1){ j++; } else if(nums[j] == 0){ var t = nums[i]; nums[i] = nums[j]; nums[j] = t; j++; i++; } else{ while(nums[k] == 2 && j < k){ k--; } var t = nums[j]; nums[j] = nums[k]; nums[k] = t; k--; } } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/lan_liang/article/details/48650121