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(荷兰国旗问题 三指针)的更多相关文章

  1. 75. Sort Colors(颜色排序) from LeetCode

      75. Sort Colors   给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...

  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 ...

  3. 刷题75. Sort Colors

    一.题目说明 题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起.难度是Medium. 二.我的解答 这个是一个排序,还是 ...

  4. 75. Sort Colors - LeetCode

    Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...

  5. 【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 ...

  6. [leetcode]75. Sort Colors三色排序

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  7. leetcode 75. Sort Colors (荷兰三色旗问题)

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  8. [LeetCode] 75. Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  9. 【一天一道LeetCode】#75. Sort Colors

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. php socket编程入门

    服务端 <?php /** * File name server.php * 服务器端代码 * * @author guisu.huang * @since 2012-04-11 * */ // ...

  2. vs的快捷键包含部分代码的自动生成

    VS2010 快捷键 全屏:Shift+Alt+Enter注释选定内容:Ctrl+E+C/Crtr+E+U代码格式化:ctrl+E+F VS2008 使用小技巧——快捷键1. 怎样调整代码排版的格式? ...

  3. 2017 CCPC 杭州 流水账

    day0: 队内训练ccpc 秦皇岛,敝校自己出的题,感觉一个星期没怎么写代码,手生得很,不出意料被打飞了. day1 (热身赛): 热身赛还算顺利,A题看有的队几分钟就草过去了,还以为又是西安ICP ...

  4. python socket 简单例子

    myserver.py: import socket serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serverso ...

  5. tree的使用

    //html <ul id="tree"></ul>  js function initTree() { $('#tree').tree({ url: '/ ...

  6. win7系统

    网址:http://www.xitongma.com/Windows7/ 使用方法:http://www.cnblogs.com/henrychan688/p/5223935.html

  7. Python捕获异常

    一.常见异常 1.语法错误:SyntaxError:invalid syntax (1)案例: (1)解决方法: ①查看代码有没有红色波浪线 ②熟悉python基本语法 2.变量名不存在:NameEr ...

  8. css 横线中间添加文字

      demoline01.02选一个用足够了 <style> .demo_line_01 { width: 200px;/*这指的是文字的宽度*/ padding: 0 20px 0; m ...

  9. Less-minxin传参

    //mixin传参 --简单传参,example: .border-radius(@radius) { -webkit-border-radius: @radius; -moz-border-radi ...

  10. 【IDEA】Maven踩坑:pom文件中的默认profiles不生效+IDEA中Maven的profiles使用说明

    一.问题即分析 项目pom文件中的profiles有3个配置:dev.test和production 默认配置的是dev,如下图: 但在本地起服务时,读取的配置始终是test里的. 二.原因 2.1 ...