75. Sort Colors(荷兰国旗问题 三指针)
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.
三个指针 lo =cur =0,hi =n-1
a[cur]==0: if(lo==cur) cur++ lo++
else: swap(lo,cur) lo++
class Solution {
public void sortColors(int[] nums) {
int begin = 0;
int cur = 0;
int end = nums.length-1;
while(cur<=end){
if(nums[cur]==2){
swap(nums,cur,end);
end--;
}
else if(nums[cur]==1){
cur++;
}
else //(nums[cur]==0)
{
if(nums[cur]!=nums[begin])
swap(nums,cur,begin);
begin++;
cur++;
}
}
}
private void swap(int[] nums,int i ,int j){
int temp;
temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
75. Sort Colors(荷兰国旗问题 三指针)的更多相关文章
- 75. Sort Colors(颜色排序) from LeetCode
75. Sort Colors 给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...
- LeetCode 75. Sort Colors (颜色分类):三路快排
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- 刷题75. Sort Colors
一.题目说明 题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起.难度是Medium. 二.我的解答 这个是一个排序,还是 ...
- 75. Sort Colors - LeetCode
Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...
- 【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三色排序
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- leetcode 75. Sort Colors (荷兰三色旗问题)
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- [LeetCode] 75. Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- 【一天一道LeetCode】#75. Sort Colors
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- swt生成、jar可执行包生成.exe可执行文件(giter)
http://tomfish88.iteye.com/blog/1074786 —————————————————————————————————————————————————————————— 最 ...
- linux在桌面和dos之间的切换
在linux 终端执行某条命令(init 5)时 提示一下错误 init :Need to be root 是提示要获取root权限 输入su 回车输入密码
- CI框架整合微信公共平台接口
#CI框架控制器 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /*** CI框架整合微信 ...
- 参考 generate-parentheses
分析: 关键:当前位置左括号不少于右括号 图是什么? 节点:目前位置左括号和右括号数(x,y)(x>=y) 边:从(x,y)到(x+1,y)和(x,y+1) ...
- 栈类Stack
Stack类是Vector类的子类.它向用户提供了堆栈这种高级的数据结构.栈的基本特性就是先进后出.即先放入栈中的元素将后被推出.Stack类中提供了相应方法完成栈的有关操作. 基本方法: publi ...
- WPF 开源项目 【watcher】 守望者,一款监控,统计,分析你每天在自己电脑上究竟干了什么的软件
时隔多年(两年),天天沉迷写PHP的我在连续加薪了逐渐发现自己不怎么写代码了. 甚至有一天我发现我连IDE 都没有打开,实在是太堕落了 为了及时悬崖勒马,回头是岸.为了鼓励自己专心写代码,我决定写一款 ...
- SharePoint 2013/2010 在一个列表或文档库内移动列表项,文档和目录位置而保持last modify by 等系统字段保持不变
本文讲述SharePoint 2013/2010 在一个列表或文档库内移动列表项.文档和目录位置而保持last modify by 等系统字段保持不变的解决方式. 近期遇到客户一个需求,在一个列表或文 ...
- Duilib教程-简单介绍
在读这篇博客的时候,可能您已经对duilib有一定的了解.所以,我并不打算对duilib进行过多的介绍.它的内核首先由外国人编写,后来由国人一个小组接过来继续编写,于是就有了现在的Duilib. 1. ...
- 面试题思考:Java 8 / Java 7 为我们提供了什么新功能
Java 7 的7个新特性 1.对集合类的语言支持: 2.自动资源管理: 3.改进的通用实例创建类型推断: 4.数字字面量下划线支持: 5.switch中使用string: 6.二进制字面量: 7.简 ...
- tinypng的python批量压缩图片功能
tinypng网站提供的图片压缩功能很不错,但是直接在网站上压缩有限制,大量压缩图片时比较麻烦,还好官方提供了很多脚本的自动化压缩接口.下面简单说下python批量压缩步骤. 1.申请api key ...