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?

    two-pass solution

     1 public class Solution {
     2     public void sortColors(int[] A) {
     3         // Start typing your Java solution below
     4         // DO NOT write main() function
     5         int len = A.length;
     6         Map<Integer, Integer> times = new HashMap<Integer, Integer>();
     7         for(int i = 0; i < len; i++){
     8             if(times.get(A[i]) != null){
     9                 times.put(A[i], times.get(A[i]) + 1);
    10             } else {
    11                 times.put(A[i], 1);
    12             }
    13         }
    14         
    15         //A = new int[len];
    16         int red = 0, white = 0, blue = 0;
    17         
    18         if(times.get(0) != null)
    19             red = times.get(0);
    20         
    21         if(times.get(1) != null)
    22             white = times.get(1);
    23         
    24         if(times.get(2) != null)
    25             blue = times.get(2);
    26             
    27         for(int i = 0; i < len; i++){
    28             if(i < red){
    29                 A[i] = 0;
    30             } else if(i >= red && i < red + white){
    31                 A[i] = 1;
    32             } else {
    33                 A[i] = 2;
    34             }
    35         }
    36     }
    37 }

     one pass

    维护两个指针:redIdx,blueIdx,从头开始扫描数组直到blueIdx(包括blueIdx)

    1.A[i]==0时,将A[i]与A[redIdx]交换,redIdx++,i++

    2.A[i]==2时,将A[i]与A[blueIdx]交换,blueIdx--,

    3.A[i]==1时,i++

     1 public class Solution {
     2     public void sortColors(int[] A) {
     3         // Start typing your Java solution below
     4         // DO NOT write main() function
     5         int len = A.length;
     6         int i = 0, redIdx = 0, blueIdx = len - 1;
     7         while(i < blueIdx + 1){
     8             if(A[i] == 0){
     9                 swap(A, i, redIdx);
    10                 redIdx ++;
    11                 i ++;
    12             } else if(A[i] == 2){
    13                 swap(A, i, blueIdx);
    14                 blueIdx --;
    15             } else{
    16                 i++;
    17             }
    18         }
    19     }
    20     
    21     public void swap(int[] A, int i, int idx){
    22         int tmp = A[i];
    23         A[i] = A[idx];
    24         A[idx] = tmp;
    25     }
    26 }
  • 相关阅读:
    CF 552(div 3) E Two Teams 线段树,模拟链表
    单词接龙
    书的复制 动规,贪心
    C++文本处理&造轮子
    【NOIP2015】【Luogu2661】信息传递(有向图最小环)
    平时上机练习的注意点(NOIP2018)
    【SHOI2009】【BZOJ2028】会场预约(线段树染色)
    【AHOI2009】【BZOJ1798】Seq 维护序列seq(线段树模板,易错提醒)
    2018 “百度之星”程序设计大赛
    2018 “百度之星”程序设计大赛
  • 原文地址:https://www.cnblogs.com/feiling/p/3256118.html
Copyright © 2011-2022 走看看