题目:

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. The file will have its original line endings in your working directory.

    在空仓库的情况下,add,出现一下问题 The file will have its original line endings in your working directory. 当报这个警告时是 ...

  2. CSUOJ 1008 Horcrux

    Description A Horcrux is an object in which a Dark wizard or witch has hidden a fragment of his or h ...

  3. Python 中的面向对象和异常处理

    在之前我们已经说过了 Python 中内置的主要的几种对象类型,(数,字符串,列表,元组和字典).而面向对象的核心人物还没出场呢 .那么我们常说的对象是什么类型的呢,其实他的类型就是“类”.继承封装和 ...

  4. javascript中switch的用法注意

    switch中文翻译过来是转换.切换的意思.用在js中,各个条件转换而执行不同代码.

  5. Oracle win32_11gR2_client.zip

    先将下载下来的ZIP文件解压,并运行setup.exe文件. 第一步:选择管理员(0MB)(A),然后点击下一步 第二步:选择语言,点击下一步 第三步:选择安装的路径,然后点击下一步 第四步:执行到第 ...

  6. Date日期

    当我们只需要一个日期时,或从系统取得,或从数据库查询,都可以放入一个Date对象. 当我们需要对Date进行详细分析,获取其中的年月日分秒各个部分的信息,用Calendar类. 当我们需要对一个字符串 ...

  7. 用 React 编写移动应用 React Native

    转载:用 React 编写移动应用 React Native ReactNative 可以基于目前大热的开源JavaScript库React.js来开发iOS和Android原生App.而且React ...

  8. anaconda安装tensorflow后pip安装jieba出错的问题

    安装jieba出错,参考https://www.cnblogs.com/minsons/p/7872647.html TypeError: parse() got an unexpected keyw ...

  9. cbv

  10. 洛谷 P4884 多少个1?

    题面在这里 好久没做题了2333,竟然还一次A了,神奇 大概就是等比数列然后把分母乘过去,然后直接BSGS就行了,就是要写快速乘恩... #include<bits/stdc++.h> # ...