LeetCode 75 Sort Colors

来源:互联网 发布:淘宝代刷网站 编辑:程序博客网 时间:2024/05/22 04:44

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?

思路一:使用HashMap,时间复杂度为O(n),空间复杂度为O(1),但是是two-pass。

public class Solution {public void sortColors(int[] A) {if (A == null || A.length < 2) return;HashMap<Integer, Integer> hashmap = new HashMap<Integer, Integer>();hashmap.put(0, 0);hashmap.put(1, 0);hashmap.put(2, 0);for (int i = 0; i < A.length; i++) {int value = hashmap.get(A[i]);value++;hashmap.put(A[i], value);}for(int i=0;i<hashmap.get(0);i++)A[i]=0;for(int i=hashmap.get(0);i<hashmap.get(0)+hashmap.get(1);i++)A[i]=1;for(int i=hashmap.get(1)+hashmap.get(0);i<A.length;i++)A[i]=2;}}

思路二:时间复杂度为O(n),空间复杂度为O(1),但是是one-pass。
public void sortColors(int[] A) {    int i=-1;    int j=-1;    int k=-1;    for(int p = 0; p < A.length; p++){        if(A[p] == 0){            A[++k]=2;            A[++j]=1;            A[++i]=0;        }else if (A[p] == 1){            A[++k]=2;            A[++j]=1;        }else  A[++k]=2;    }}
2014.1.5代码优化
import java.util.Arrays;import java.util.HashMap;public class Solution {public void sortColors(int[] A) {if (A == null || A.length < 2) return;HashMap<Integer, Integer> hashmap = new HashMap<Integer, Integer>();hashmap.put(0, 0);hashmap.put(1, 0);hashmap.put(2, 0);for (int i = 0; i < A.length; i++) {int value = hashmap.get(A[i]);value++;hashmap.put(A[i], value);}Arrays.fill(A,0,hashmap.get(0),0);Arrays.fill(A,hashmap.get(0),hashmap.get(0)+hashmap.get(1),1);Arrays.fill(A,hashmap.get(0)+hashmap.get(1),A.length,2);}}
---------------------------2015.1.21更新--------------------------------

设置两个标志位begin和end分别指向这个数组的开始和末尾,然后用一个标志位current从头开始进行遍历:

1)若遍历到的位置为0,则说明它一定属于前部,于是就和begin位置进行交换,然后current向前进,begin也向前进(表示前边的已经都排好了)。

2)若遍历到的位置为1,则说明它一定属于中部,根据总思路,中部的我们都不动,然后current向前进。

3)若遍历到的位置为2,则说明它一定属于后部,于是就和end位置进行交换,由于交换完毕后current指向的可能是属于前部的,若此时current前进则会导致该位置不能被交换到前部,所以此时current不前进。而同1),end向后退1。

public void sortColors(int[] A) { int current = 0;      int end = A.length-1;      int begin = 0;      int temp=-1;while(begin<end){if(A[current]==0){temp=A[begin];A[begin]=A[current];A[current]=temp;begin++;current++;}else if(A[current]==1){current++;}else {temp=A[end];A[end]=A[current];A[current]=temp;end--;}}}


本题原为荷兰三色国旗问题,参考链接:
http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Sort/Flag/
http://blog.sina.com.cn/s/blog_4c98b96001000862.html
http://blog.csdn.net/feliciafay/article/details/19024897
http://hi.baidu.com/de1zhanjie/item/bdac2bbab11d12afeaba9355


0 0
原创粉丝点击