问题描述: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.

就是把三种颜色相同的放在一起,0,1,2,代表三种颜色,按0,1,2排序。

算法分析:其实就是数组排序的问题,但是数组元素只有三种,所以,可以采用快速排序的思想,设置两个指针,先把0颜色放在最左边,然后把1颜色放在次左边。

也可以直接使用快速排序,只不过时间复杂度有点高,并且栈溢出。

public class SortColors
{
public static void sortColors(int[] nums)
{
int start = 0;
int end = nums.length - 1;
while(start <= end)//把0都放在左边
{
if(nums[start] != 0)
{
if(nums[end] == 0)
{
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start ++;
end --;
}
else
{
end --;
}
}
else
{
start ++;
}
} end = nums.length - 1;
while(start <= end)//把1都放在左边
{
if(nums[start] != 1)
{
if(nums[end] == 1)
{
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start ++;
end --;
}
else
{
end --;
}
}
else
{
start ++;
}
}
} //快速排序
public static void sortColors2(int[] nums)
{
quickSort(nums, 0, nums.length - 1);
}
public static void quickSort(int[] nums, int left, int right)
{
int leftIndex = left;
int rightIndex = right;
int pivot = nums[(leftIndex + rightIndex)/2];
while(leftIndex <= rightIndex)
{
while(nums[leftIndex] < pivot)
{
leftIndex ++;
}
while(nums[rightIndex] > pivot)
{
rightIndex --;
}
if(leftIndex <= rightIndex)
{
int temp = nums[leftIndex];
nums[leftIndex] = nums[rightIndex];
nums[rightIndex] = temp;
leftIndex ++;
rightIndex --;
}
if(leftIndex < right)
{
quickSort(nums, leftIndex, right);
}
if(rightIndex > left)
{
quickSort(nums, left, rightIndex);
}
}
}
}

Sort Colors,颜色排序的更多相关文章

  1. 75. Sort Colors(颜色排序) from LeetCode

      75. Sort Colors   给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...

  2. [LeetCode] Sort Colors 颜色排序

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

  3. leetCode 75.Sort Colors (颜色排序) 解题思路和方法

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

  4. [LeetCode] 75. Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  5. LeetCode 75. Sort Colors(排序颜色)

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

  6. LeetCode OJ:Sort Colors(排序颜色)

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

  7. Leetcode75. Sort Colors颜色分类

    给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白色和蓝色. ...

  8. leetcode 75 Sort Colors 计数排序,三路快排

    解法一:计数排序:统计0,1,2 的个数 时间复杂度:O(n) 空间复杂度:O(k)    k为元素的取值范围, 此题为O(1) class Solution { public: void sortC ...

  9. [LeetCode] 324. Wiggle Sort II 摆动排序 II

    Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...

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

    [075-Sort Colors (颜色排序)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array with n objects colore ...

随机推荐

  1. css3动画效果:3 3D动画

    立方体旋转动画效果 css #container{ width: 400px; height: 400px; ; ; -webkit-perspective-origin:50% 225px; per ...

  2. C# .net 数组倒序排序

    1.数组方法 Array.Sort(Array Array);  此方法为数组的排序(正序)方法 Array.Reverse(Array Array);  此方法可以将数组中的值颠倒 两个方法结合使用 ...

  3. TADDConnetion组件,TADOQuery

    一.TADDConnetion 二.TADOQuery 1.RecNo:从1开始 当前记录行数;ADOQuery1.RecNo 选择后一行数据集内容:ADOQuery1.RecNo:=ADOQuery ...

  4. Oracle安装错误:File not found WFMLRSVCApp.ear

    oracle 11g安装过程中问题:找不到WFMLRSVCApp.ear 在 oracle 11gR2 64bit 安装到window 7 64位操作系统中,安装到53%时,提示找不到WFMLRSVC ...

  5. Howto: Performance Benchmarks a Webserver

    Howto: Performance Benchmarks a Webserver last updated June 9, 2006 in CategoriesApache, FreeBSD, Ho ...

  6. html5.js让IE(包含IE6)支持HTML5元素方法

    原文地址:http://blog.sina.com.cn/s/blog_62a36ec401018oqb.html html5.js让IE(包含IE6)支持HTML5元素方法 微软的最新浏览器IE8及 ...

  7. 011-JDK可视化监控工具-Jstat

    一.概述 Jstat 是JDK自带的一个轻量级小工具.全称“Java Virtual Machine statistics monitoring tool”,它位于java的bin目录下,主要利用JV ...

  8. 关于shared pool的深入探讨(三)

    基本命令: ALTER SESSION SET EVENTS 'immediate trace name LIBRARY_CACHE level LL'; 其中LL代表Level级别,对于9.2.0及 ...

  9. 阿里、腾讯、京东、微软,各家算法&数据挖掘岗位面经大起底!

    阿里.腾讯.京东.微软,各家算法&数据挖掘岗位面经大起底! 2016-02-24 36大数据 36大数据 作者: 江少华 摘要: 从2015年8月到2015年10月,花了3个月时间找工作,先后 ...

  10. Delphi 正则表达式起步

    Delphi 正则表达式起步 在 Delphi 中使用正则表达式, 目前 PerlRegEx 应该是首选, 准备彻底而细致地研究它. 官方网站: http://www.regular-expressi ...