LeetCode OJ平台Sort Colors讨论主题算法
2015-08-13 17:30
483 查看
原题如下面,这意味着无序排列(由0,1,2组成)。一号通。组织成若干阵列0-几个1-几个2这样的序列。
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.
click to show follow up.
Follow up:
A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
Could you come up with an one-pass algorithm using only constant space?
想了一段时间,未果,于是前往Discuss区域围观。
帖子网址点击打开链接
网友xianlei给出了一个精妙的方法。我理解之后,写出代码例如以下,由于有具体的凝视。就不啰嗦了。
在草纸上模拟一下,或者单步调试一下。就会明确这种方法的精妙之处!
!
网友lchen77提供了还有一个非常好的思路,我的实现代码例如以下。
由于题目要求依照0,1,2的顺序来排序,所以遇到0。与前面的A[k++]交换。遇到2与A[j--]交换。
须要注意的是,A[k]要么等于0,要么等于1,所以0与A[k++]交换之后。i不用--,整个前面的序列是合法的。
可是2与A[j--]交换之后。i要--。由于从数组尾部随便一个数字与当前的2换完之后,必须还要验证。以防不合法。
并且,循环的终止条件是i,j相遇,相遇的时候,还要运行一次,即循环终止条件是i>j
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.
click to show follow up.
Follow up:
A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
Could you come up with an one-pass algorithm using only constant space?
想了一段时间,未果,于是前往Discuss区域围观。
帖子网址点击打开链接
网友xianlei给出了一个精妙的方法。我理解之后,写出代码例如以下,由于有具体的凝视。就不啰嗦了。
在草纸上模拟一下,或者单步调试一下。就会明确这种方法的精妙之处!
!
public class Solution { static int[] A = {1,0}; public static void sortColors(int[] A) { int i = 0;//last index of array 0 int j = 0;//last index of array 1 int k = 0;//last index of array 2 for(int m = 0; m < A.length; m ++){ if(A[m] == 0){ // add 2 to the end of 2 array, replace the 1st 2 with 1, then insert 0 into 0 array, A[k++] = 2; A[j++] = 1; A[i++] = 0; } else if(A[m] == 1){// add 2 to the end of 2 array, then insert 1, at the location of the 1st 2 A[k++] = 2; A[j++] = 1; } else{//add 2 to the end of 2 array A[k++] = 2; } } } public static void main(String args[]){ sortColors(A); } }
网友lchen77提供了还有一个非常好的思路,我的实现代码例如以下。
由于题目要求依照0,1,2的顺序来排序,所以遇到0。与前面的A[k++]交换。遇到2与A[j--]交换。
须要注意的是,A[k]要么等于0,要么等于1,所以0与A[k++]交换之后。i不用--,整个前面的序列是合法的。
可是2与A[j--]交换之后。i要--。由于从数组尾部随便一个数字与当前的2换完之后,必须还要验证。以防不合法。
并且,循环的终止条件是i,j相遇,相遇的时候,还要运行一次,即循环终止条件是i>j
public class Solution { public static void sortColors(int[] A){ int k = 0; //index for 0 array; int j = A.length-1;//index for 2 array for(int i = 0; i <= j;i ++){ if(A[i] == 1){ continue; } else if(A[i] == 0){ A[i] = A[k]; A[k++] = 0; } else{ A[i] = A[j]; A[j--] = 2; i --; } } } }
相关文章推荐
- 学习JAVA框架的心得体会
- kubernetes API Server 权限管理实践
- Android和服务器通信
- java输出获取英文字母
- 2015/8/13/客户端服务器数据库综合
- iOS设计模式汇总
- 程序员日记我们需要有条理的生活
- 【iOS官方文档翻译】UICollectionView与UICollectionViewFlowLayout
- iphone5 ios7.1 上下有黑边 解决方案
- LeetCode解题报告--Palindrome Number
- UIAlertView提示框自动消失
- UITabBarController — 标签视图控制器
- 优化Apache,提高并发
- js判断访问设备是手机端还是PC端
- 将对象里属性值相同的元素放到同一容器里
- ZOJ 1372 Networking 最小生成树 Kruscal && Prim
- HDOJ-2835(最佳置换算法)
- 时钟体系
- asp.net(C#)遍历memcached缓存对象
- 实战Memcached缓存系统(1)Memcached基础及示例程序