zoukankan      html  css  js  c++  java
  • 【leetcode】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.

    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?


    题解:题目中给出了借助count sort的方法遍历数组两遍。如果想只遍历数组一遍就解决这个问题,可以借助快排里面的partition思想。以1作为轴,对整个数组进行一次partition,那么1的左边就是小于等于1的元素,右边就全部是2了。
    其中zero的含义就是下一个0应该放的地方,two的含义就是下一个2应该放的地方。此时,指针在往前走的时候,如果遇到0,就和zero所指的元素换位;如果遇到2,就和two所指的元素换位。特别要注意的一点如代码第18行所示,在和two所指的元素换位以后指针i不能往前走,因为有可能这时和2交换的元素是0,而这个0还没有到达它应该到达的位置。比如1 2 0这个数组,第一次交换后数组变成1 0 2,此时指针在i处不能往前走,要在下一步和1交换得到0 1 2后才能继续往前走。所以18行代码后面没有i++的操作。
    例如序列0 1 2 2 1 1 2,partition的过程如下图所示:
     
    代码如下:
     1 class Solution {
     2 public:
     3     void sortColors(int A[], int n) {
     4         if(n == 0)
     5             return;
     6         int zero = 0,two = n-1;
     7         for(int i = 0;i <= two;){
     8             if(A[i] == 0){
     9                 swap(A[i],A[zero]);
    10                 zero++;
    11                 i++;
    12             }
    13             else if(A[i] == 1)
    14                 i++;
    15             else if(A[i] == 2){
    16                 swap(A[i],A[two]);
    17                 two--;
    18             }
    19         }
    20     }
    21 };

     JAVA版本代码:

     1 public class Solution {
     2     int[] swap(int[] A,int i,int j){
     3         int temp = A[i];
     4         A[i] = A[j];
     5         A[j] = temp;
     6         return A;
     7     }
     8     public void sortColors(int[] A) {
     9         if(A.length == 0)
    10             return;
    11         int zero = 0;
    12         int two = A.length - 1;
    13         for(int i = 0;i <= two;){
    14             if(A[i] == 2){
    15                 swap(A, i, two);
    16                 two--;
    17             }
    18             
    19             else if(A[i] == 1)
    20                 i++;
    21             
    22             else if(A[i] == 0)
    23             {
    24                 swap(A, i, zero);
    25                 zero++;
    26                 i++;
    27             }
    28         }
    29     }
    30 }
     
  • 相关阅读:
    activiti串行会签的使用
    Linux中shell字符串分隔、字符串替换、字符串拼接
    spring 通过启动命令配置文件路径
    流程activiti的组和用户的使用
    使用activiti的designer插件记录
    windows下vue+webpack前端开发环境搭建及nginx部署
    node.js的安装
    开启我的博客之旅
    Docker 命令集合
    github免费搭建个人博客,拥有免费域名
  • 原文地址:https://www.cnblogs.com/sunshineatnoon/p/3780364.html
Copyright © 2011-2022 走看看