题目:

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?

题解:

这道题就是运用指针来解决了,可以说叫3指针吧。

一个指针notred从左开始找,指向第一个不是0(红色)的位置;一个指针notblue从右开始往左找,指向第一个不是2(蓝色)的位置。

然后另一个新的指针i指向notred指向的位置,往后遍历,遍历到notred的位置。

这途中需要判断:

当i指向的位置等于0的时候,说明是红色,把他交换到notred指向的位置,然后notred++,i++。

当i指向的位置等于2的时候,说明是蓝色,把他交换到notblue指向的位置,然后notred--。

当i指向的位置等于1的时候,说明是白色,不需要交换,i++即可。

代码如下:

 1     public static void swap(int A[], int i, int j){
 2         int tmp = A[i];
 3         A[i]=A[j];
 4         A[j]=tmp;
 5     }
 6     
 7     public static void sortColors(int A[]) {
 8         if(A == null || A.length==0)
 9             return;
             
         int notred=0;
         int notblue=A.length-1;
          
         while (notred<A.length&&A[notred]==0)
             notred++;
             
         while (notblue>=0&&A[notblue]==2)
             notblue--;
          
         int i=notred;
         while (i<=notblue){
             if (A[i]==0) {
                 swap(A,i,notred);
                 notred++;
                 i++;
             }else if (A[i]==2) {
                 swap(A,i,notblue);
                 notblue--;
             }else
                 i++;
         }
     }

Sort Colors leetcode java的更多相关文章

  1. 75. Sort Colors - LeetCode

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

  2. Sort Colors [LeetCode]

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  3. Sort Colors —— LeetCode

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  4. Sort List leetcode java

    题目: Sort a linked list in O(n log n) time using constant space complexity. 题解: 考虑到要求用O(nlogn)的时间复杂度和 ...

  5. Insertion Sort List Leetcode java

    题目: Sort a linked list using insertion sort. 题解: Insertion Sort就是把一个一个元素往已排好序的list中插入的过程. 初始时,sorted ...

  6. LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors

    1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...

  7. LeetCode 75. 颜色分类(Sort Colors) 30

    75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中, ...

  8. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  9. [Leetcode Week2]Sort Colors

    Sort Colors题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/sort-colors/description/ Description Give ...

随机推荐

  1. Microsoft Office Access

    Microsoft Office Access各版本下载地址:http://www.accessoft.com/download.html 简介 access(微软发布的关联式数据库管理系统)一般指M ...

  2. CentOS日志的简单介绍

    在CentOS7中,系统的日志消息由两个服务负责处理:system-journald和rsyslog. (1).常见的日志及作用 /var/log目录里存放了一些特定于系统和服务的日志文件,由rsys ...

  3. java 不通过第三个字符串,实现一个字符串倒序

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha String s="abcde"; String  s2 = new ...

  4. [Arc058E] Iroha and Haiku

    [Arc058E] Iroha and Haiku 题目大意 问有多少\(n\)个数的正整数序列,每个数在\([1,10]\)之间,满足存在\(x,y,z,w\)使得\(x\to y-1,y\to z ...

  5. [POI2015]Wilcze doły

    [POI2015]Wilcze doły 题目大意: 给定一个长度为\(n(n\le2\times10^6)\)的数列\(A(1\le A_i\le10^9)\),可以从中选取不超过\(d\)个连续数 ...

  6. Java并发(十九):final实现原理

    final在Java中是一个保留的关键字,可以声明成员变量.方法.类以及本地变量. 一旦你将引用声明作final,你将不能改变这个引用了,编译器会检查代码,如果你试图将变量再次初始化的话,编译器会报编 ...

  7. EXPLAIN 用法

    重点是第二种用法,需要深入的了解. 先看一个例子: mysql> explain select * from t_order; +----+-------------+---------+--- ...

  8. 趁热打铁学node

    前言 不废话直接官网下载安装. windows安装很简单,双击ok. 完成后命令行输入node -v. 会提示版本号,说明安装成功(Node.js 还自动附带安装了 npm,类似ruby的gem). ...

  9. spring---aop(8)---Spring AOP中optimize

    写在前面 optimize是ProxyConfig的属性.意思为 是否对生产代理策略使用优化. public class ProxyConfig implements Serializable { p ...

  10. 小米路由通过SSH添加静态路由表之后无法跳转的问题

    1.确定系统已经开启了转发功能: /etc/sysctl.conf下的配置项目为net.ipv4.ip_forward = 1 2.关闭防火墙的REJECT,也就是修改/etc/config/fire ...