LeetCode Sort Colors(排序)

原创 2015年07月10日 22:53:09

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?

题意:给出由0,1,2三个数组成的数组,要求用时间复杂度为O(n),空间复杂度为O(1)

思路:分别从两边搜索,从左边找到第一个不为0的位置,从右边找到第一个不为2的位置,然后从这两个位置开始查找,如果为0,将其与左边的数替换,如果为2,将其与右边的数替换

代码如下:

public class Solution {
    public void sortColors(int[] nums) {
         int left = 0, right = nums.length - 1;

        while (left <= right && nums[left] == 0) left++;
        while (right >= 0 && nums[right] == 2) right--;

        if (left >= right) return;

        int tmp;
        int a = left;
        while (a <= right) {
            if (nums[a] == 0) {
                tmp = nums[a];
                nums[a] = nums[left];
                nums[left] = tmp;
                a++;
                left++;
            } else if (nums[a] == 2) {
                tmp = nums[right];
                nums[right] = nums[a];
                nums[a] = tmp;
                while (nums[right] == 2) right--;
                if (nums[a] == 1) a++;
            } else a++;
        }
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LeetCode-Sort Colors-排序

Sort Colors Java 排序 Given an array with n objects colored red, white or blue, sort them so that obj...
  • KINGHMY
  • KINGHMY
  • 2016年09月12日 21:41
  • 223

LeetCode-75. Sort Colors (JAVA)三色排序

LeetCode-75. Sort Colors (JAVA)三色排序

[C++]LeetCode: 127 Sort Colors (计数排序 & 快速排序)

题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same...

【LeetCode-面试算法经典-Java实现】【075-Sort Colors (颜色排序)】

【075-Sort Colors (颜色排序)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given an array with n objects colored ...

LeetCode 75 Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same colo...

LeetCode(75)Sort Colors (荷兰三色旗问题 Dutch National Flag)

题目如下: The flag of the Netherlands consists of three colours: red, white and blue. Given balls of the...

LeetCode:Sort Colors

推荐参照:Leetcode题目难度等级及面试频率总结题目描述:  Given an array with n objects colored red, white or blue, sort them...

[leetCode 75] Sort Colors

题目链接:sort-colors import java.util.Arrays; /** * Given an array with n objects colored red...
  • ever223
  • ever223
  • 2015年03月20日 14:38
  • 784

Leetcode_75_Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same colo...

【leetcode 数组】Sort Colors

1、题目 2、分析 3、代码
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode Sort Colors(排序)
举报原因:
原因补充:

(最多只允许输入30个字)