zoukankan      html  css  js  c++  java
  • [LeetCode] Sort Colors 只有3个类型的排序

    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.

    click to show follow up.

    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?

    Hide Tags
     Array Two Pointers Sort
     
     
      这题还是挺简单的,就是对只有3个类型(0 1 2)的排序,题目中给了一个两次遍历的排序方法,即统计出现的次数然后填回数组,要求是遍历一次完成,看了一下discuss ,实现逻辑类似,即写的如何。
     
    思路:
    1. 创建一个zeroidx 表示0 的末尾下标,初始化为0,twoidx 表示2的下标,初始化为n,不同地方是前者操作时++,后者--。
    2. 遍历数组如果遇到1,则继续遍历。
    3. 如果遇到0,则替换 zeroidx 与i 上的值,然后zerosidx +1,如果此时该位置上的值位0,则数组从0 to i 上为0,所以zeroidx = i+1.
    4. 如果遇到2,则 twoidx -1, 然后替换 twoidx 与i 的值,因为替换后的i 位置上的值未判断,所以i-1 进行多一次该位置上的遍历。

    3.中zeroidx 可以一直+1 不直接跳位,这样需要多次交换,discuss 实现多是这样。

    下面是我写的代码:

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class Solution {
     5 public:
     6     void sortColors(int A[], int n) {
     7         int zeroIdx = 0;
     8         int twoIdx = n;
     9         for(int i =0;i<n&&i<twoIdx;i++){
    10             if(A[i]==1){
    11                 continue;
    12             }
    13             if(A[i]==0){
    14                 A[i] = A[zeroIdx];
    15                 A[zeroIdx] = 0;
    16                 if(++zeroIdx<n&&A[zeroIdx]==0)    zeroIdx=i+1;
    17             }
    18             else if(A[i]==2){
    19                 twoIdx--;
    20                 A[i]=A[twoIdx];
    21                 A[twoIdx]=2;
    22                 i--;
    23             }
    24             else
    25                 return ;
    26         }
    27         return ;
    28     }
    29 };
    30 
    31 int main()
    32 {
    33     int a[] = {1,2,0,1};
    34     Solution sol;
    35     sol.sortColors(a,sizeof(a)/sizeof(int));
    36     for(int i=0;i<sizeof(a)/sizeof(int);i++)
    37         cout<<a[i]<<" ";
    38     cout<<endl;
    39     return 0;
    40 }
    View Code

    discuss 中有一个实现非常不错,逻辑清晰,可以将变量数3 扩展为k 个,只要不怕写起来麻烦,其逻辑类似于插入排序,将遍历的项插入正确的位置:

    1. 为3个(k个) 变量创建index a b c= -1;
    2. 遍历数组,如果为0,顺序修改a[++c] a[++b] a[++a],这样如果abc 一样,最终只有修改了 a[++a] 这一项,然后同时又更新了3者的idx。
    3. 如果为1,则顺序修改 a[++c] a[++b],这样 0的index 未变。
    4. 如果为2,则顺序修改 a[++c]。
     1  public void sortColors(int[] A) {
     2 
     3 
     4     int i=-1, j=-1, k=-1;
     5 
     6     for(int p = 0; p < A.length; p++)
     7     {
     8         if(A[p] == 0)
     9         {
    10             A[++k]=2;
    11             A[++j]=1;
    12             A[++i]=0;
    13         }
    14         else if (A[p] == 1)
    15         {
    16             A[++k]=2;
    17             A[++j]=1;
    18 
    19         }
    20         else if (A[p] == 2)
    21         {
    22             A[++k]=2;
    23         }
    24     }
    25 
    26 }
    View Code
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    iOS学习6:CoreGraphics简单绘图
    什么是GCD
    iOS对象序列化
    Swift中基本类型的转换
    DataTable转List
    .Net Mvc Automated Migration 数据迁移
    .Net WebApi基本操作
    Sql Server日期时间格式转换
    jquery ajax POST 例子详解
    发布.net mvc遇到的HTTP错误 403.14Forbidden解决办法
  • 原文地址:https://www.cnblogs.com/Azhu/p/4129351.html
Copyright © 2011-2022 走看看