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 }
  • 相关阅读:
    学习新东西 方法
    转 Dock 外 命令解析
    转 Dockerfile 常用指令
    RPC应用的java实现(转)
    link with editor
    org.xml.sax.SAXParseException: prolog 中不允许有内容
    webservice
    logging.xml file setfile(null,true) call failed
    log4j配置 logging.xml (转载)
    tomcat server.xml docbase workdir
  • 原文地址:https://www.cnblogs.com/feiling/p/3256118.html
Copyright © 2011-2022 走看看