LeetCode -- Sort Colors
2015-09-22 09:56
267 查看
题目描述
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note:
You are not suppose to use the library's sort function for this problem.
其实就是三色旗问题,给3个数字0,1,2,把它们排序好。
解法一:
三次遍历。
思路:
先从左边找2,右边找0,第一次遍历,保证0在2的左边;
从左边找1,右边找0,第二次遍历,保证0在1的左边;
从左边找2,右边找1,第三次遍历,保证1在2的左边。
实现代码:
解法二:
其实本题按区域来理解,只需要遍历一次。
思路:
i,j,k分别表示0,1,2的区域下标
1. 如果j为1,往下走:j++ ;如果j为0,交换nums[i]和nums[j]并i++,j++。第一步是保证0区域在1区域的左边
2. 如果j为2,从右边(k=length-1,k--)开始找,找到不是2的,交换nums[j]和nums[k]并k--,这一步是为了保证2在0与1区域的右边。
实现代码:
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note:
You are not suppose to use the library's sort function for this problem.
其实就是三色旗问题,给3个数字0,1,2,把它们排序好。
解法一:
三次遍历。
思路:
先从左边找2,右边找0,第一次遍历,保证0在2的左边;
从左边找1,右边找0,第二次遍历,保证0在1的左边;
从左边找2,右边找1,第三次遍历,保证1在2的左边。
实现代码:
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; } } } }
解法二:
其实本题按区域来理解,只需要遍历一次。
思路:
i,j,k分别表示0,1,2的区域下标
1. 如果j为1,往下走:j++ ;如果j为0,交换nums[i]和nums[j]并i++,j++。第一步是保证0区域在1区域的左边
2. 如果j为2,从右边(k=length-1,k--)开始找,找到不是2的,交换nums[j]和nums[k]并k--,这一步是为了保证2在0与1区域的右边。
实现代码:
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--; } } } }
相关文章推荐
- iOS中3种基础的数据持久化模式
- Mysql导入大表文件时注意修改参数
- 有关流Stream
- 校园讲座小记
- Spinner类setSelection执行流程源码解析
- 媒体变现四种方式
- 【转】10分钟搭建 App 主流框架
- 学习小记
- /dev/mapper/VolGroup-lv_root: unexpected inconsistency; run fsck manually
- 关于Latch
- Linux Shell 文本处理工具集锦
- 顶场先?底场先?
- 理解矩阵乘法
- php+mysql新无线级分类
- 很赞的源码平台
- mysql多实例
- XE8-indy10中CheckForGracefulDisconnect函数源码
- Aspose.Cell 导出和导入Excel
- SVN:冲突解决 合并别人的修改
- QQ协议分析及还原 http://blog.csdn.net/qinggebuyao/article/details/7814499