码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode: Sort Colors 解题报告

时间:2014-10-24 18:38:49      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   使用   java   

bubuko.com,布布扣

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?

Solution 1:

类似radix sort, 先扫描一次得知所有的值出现的次数,再依次setup它们即可

ref: http://blog.csdn.net/fightforyourdream/article/details/15025713

bubuko.com,布布扣
 1 public void sortColors1(int[] A) {
 2         if (A == null || A.length == 0) {
 3             return;
 4         }
 5         
 6         int len = A.length;
 7         
 8         int red = 0;
 9         int white = 0;
10         int blue = 0;
11         
12         for (int i = 0; i < len; i++) {
13             if (A[i] == 0) {
14                 red++;    
15             } else if (A[i] == 1) {
16                 white++;
17             } else {
18                 blue++;
19             }
20         }
21         
22         for (int i = 0; i < len; i++) {
23             if (red > 0) {
24                 A[i] = 0;
25                 red--;
26             } else if (white > 0) {
27                 A[i] = 1;
28                 white--;
29             } else {
30                 A[i] = 2;
31             }
32         }
33     }
View Code

Solution 2:

使用双指针指向左边排好的0和右边排好的1,再加一个指针cur扫描整个数组。一趟排序下来就完成了。相当快捷。

注意:与右边交换之后,cur不能移动,因为你有可能交换过来是1或是0,还需要与左边交换。而与左边交换后,cur就可以向右边移动了。

 

bubuko.com,布布扣
 1 public void sortColors(int[] A) {
 2         if (A == null || A.length == 0) {
 3             return;
 4         }
 5         
 6         int len = A.length - 1;
 7         int left = 0;
 8         int right = len;
 9         
10         int cur = 0;
11         while (cur <= right) {
12             if (A[cur] == 2) {
13                 // 换到右边,换过来的有可能是0,也有可能是1,所以cur要停留
14                 
15                 swap(A, cur, right);
16                 right--;
17             } else if (A[cur] == 0) {
18                 // 从左边换过来的只可能是1,所以可以直接cur++
19                 // 因为所有的2全部换到右边去了。
20                 
21                 swap(A, cur, left);
22                 left++;
23                 cur++;
24             } else {
25                 cur++;
26             }
27         }
28     }
29     
30     public void swap(int[] A, int n1, int n2) {
31         int tmp = A[n1];
32         A[n1] = A[n2];
33         A[n2] = tmp;
34     }
View Code

 

GitHub:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/sort/SortColors.java

 

LeetCode: Sort Colors 解题报告

标签:style   blog   http   color   io   os   ar   使用   java   

原文地址:http://www.cnblogs.com/yuzhangcmu/p/4048668.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!