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.
    
    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?

    难度:naive做法60,计数排序

     1 public class Solution {
     2     public void sortColors(int[] A) {
     3         if (A == null || A.length == 0) return; 
     4         int[] colornum = new int[3];
     5         for (int elem : A) {
     6             if (elem == 0) colornum[0]++;
     7             if (elem == 1) colornum[1]++;
     8             if (elem == 2) colornum[2]++;
     9         }
    10         for (int i=0; i<colornum[0]; i++) {
    11             A[i] = 0;
    12         }
    13         for (int i=colornum[0]; i<colornum[0]+colornum[1]; i++) {
    14             A[i] = 1;
    15         }
    16         for (int i=colornum[0]+colornum[1]; i<A.length; i++) {
    17             A[i] = 2;
    18         }
    19     }
    20 }

    scan两次也是一种办法,第一次把1和2当做一种颜色,放到0的右边,第二次0已经全部在左边,只需把2放到1右边

    我们考虑怎么用一次扫描来解决。

    Best Solution(Discuss里vote最高): the basic idea is to use two pointer low and high and an iterator i

    For every elem, low pointer is where 0 should be put, high pointer is where 2 should be put

    when i meet high, then the whole array is already sorted(注意i到high的时候还的最后再看看能不能交换,因为high是2应该被放的地方,可能目前还是0)

    我的理解:

    low: 0应该被放的位置(已sorted的数组0结束位置的下一位)

    i: 1应该被放的位置(已sorted的数组1结束位置的下一位)

    high: 2应该被放的位置(从后向前,2结束位置的前一位)

    所以i == high了还要再继续处理一次,因为当前元素还没有被处理过

     1    public void sortColors(int[] A) {
     2        if(A==null || A.length<2) return;
     3        int low = 0; 
     4        int high = A.length-1;
     5        for(int i = low; i<=high;) {
     6            if(A[i]==0) {
     7               // swap A[i] and A[low] and i,low both ++
     8               int temp = A[i];
     9               A[i] = A[low];
    10               A[low]=temp;
    11               i++;low++;
    12            }else if(A[i]==2) {
    13                //swap A[i] and A[high] and high--;
    14               int temp = A[i];
    15               A[i] = A[high];
    16               A[high]=temp;
    17               high--;
    18            }else {
    19                i++;
    20            }
    21        }
    22    }
  • 相关阅读:
    MVC之Control和View之间传递数据
    与 BUG 跟踪系统/问题跟踪集成
    与 BUG 跟踪系统/问题跟踪集成
    版本库浏览器
    c#创建快捷方式代码
    项目中用到的数字证书的创建,签名实现
    vs2005 "automation服务器不能创建对象"解决方法.
    项目中用到的数字证书的创建,签名实现
    创建一个输入标识符 也就是一个输入的光标
    利用设备描述符画图
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/3981231.html
Copyright © 2011-2022 走看看