LeetCode OJ平台Sort Colors讨论主题算法
原题如下面,这意味着无序排列(由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.
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 --;
}
}
}
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
LeetCode OJ平台Sort Colors讨论主题算法的更多相关文章
- LeetCode OJ 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- LeetCode OJ:Sort Colors(排序颜色)
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 【LeetCode】75. Sort Colors (3 solutions)
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 【一天一道LeetCode】#75. Sort Colors
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】75. Sort Colors 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 计数排序 双指针 日期 题目地址:https://l ...
- 【LeetCode】075. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- leetcode笔记:Sort Colors
一. 题目描写叙述 Given an array with n objects colored red, white or blue, sort them so that objects of the ...
- LeetCode OJ:Sort List(排序链表)
Sort a linked list in O(n log n) time using constant space complexity. 题目要求在常数控件内以O(nlogn)的事件复杂度来排序链 ...
- 【leetcode】75. Sort Colors
题目如下: 解题思路:我的解题思路是遍历数组,遇到0删除该元素并插入到数组头部,遇到1则不处理,遇到2删除该元素并插入到数组尾部. 代码如下: class Solution(object): def ...
随机推荐
- 4、linux开发中常用指令
1.cat /proc/device 可以查看各个全部字符设备和块设备,在register_chrdev中设置的名字在打印出来的信息中可以看到:2.top 可以看各个应用程序占用CPU量及PID等信息 ...
- JS中给函数参数添加默认值(多看课程)
JS中给函数参数添加默认值(多看课程) 一.总结 一句话总结:咋函数里面是可以很方便的获取调用函数的参数的,做个判断就好,应该有简便方法,看课程. 二.JS中给函数参数添加默认值 最近在Codewar ...
- Booth算法
Booth算法 算法描述(载自维基百科) 对于N位乘数Y,布斯算法检查其2的补码形式的最后一位和一个隐含的低位,命名为y-1,初始值为0.对于yi, i = 0, 1, ..., N - 1,考察yi ...
- iOS开发Quartz2D之八:图形上下文状态栈
#import "DrawView.h" @implementation DrawView - (void)drawRect:(CGRect)rect { // Drawing c ...
- 转载:使用bat命令来快速安装和卸载Service服务
一般我们在编写完Service服务程序后,都是通过cmd命令提示窗口来安装或卸载服务,但频繁的在cmd窗口中去“拼”文件的路径着实让人“不能忍”.所以,我们需要一钟“更快捷”的方式来进行安装或者卸载操 ...
- 【t035】收入计划
Time Limit: 1 second Memory Limit: 32 MB [问题描述] 高考结束后,同学们大都找到了一份临时工作,渴望挣得一些零用钱.从今天起,Matrix67将连续工作N天( ...
- 【C#】万事开头难(二)<机房重构>
前言 机房将要进行完了,之所以仍然使用这个题目,是由于我想告诉自己.无论面对多么未知的事物.不要害怕,去做就好.在这么多天的机房重构中,发现了好多问题,也攻克了好多问题,今天,就把我解决的问题分享给大 ...
- Web开发之分页算法,N(N>=3)种解决方案
在Web开发中,分页是最常用的一个功能了.但是,想简单.灵活.方便地解决分页这个问题,还是需要动点脑筋的. 今天,简要梳理下,过去6年中,亲自遇到的分页问题和解决办法. 1. 定义问题 原由:在页面中 ...
- 项目中使用了个quartz包,启动时提示Quartz version update check failed
<span style="font-family: Arial, Helvetica, sans-serif;">2015-09-13 00:12:02 Abstrac ...
- 【Material Design视觉设计语言】开篇
作者:郭孝星 微博:郭孝星的新浪微博 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells Github:https://github.co ...