问题描述: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. CSS 伪元素 使用参考

    伪元素可以做得事情是非常多的,详情大家可以参考这里 大放异彩的伪元素——可以做什么? 本篇主要讲两个伪元素:before和:after的几个要点: 1.:before和:after是加在元素的里面,也 ...

  2. mongoDB-----针对某个或多个文档只需要部分更新可使用原子的更新修改器

    update() db.collection.update( <query>, <update>, { upsert: <boolean>, multi: < ...

  3. CListCtrl消息及解释

    对于CListCtrl消息的解释:[来自网络]LVN_BEGINDRAG 鼠标左键正在被触发以便进行拖放操作(当鼠标左键开始拖拽列表视图控件中的项目时产生) LVN_BEGINRDRAG 鼠标右键正在 ...

  4. win7下设置smtp的方法

    在win7中开启SMTP服务的方法如下: 1. 首先确定是否开启IIS服务:在运行中输入“inetmgr”,若提示出错,则表明未安装.进入步骤2,否则进入步骤3: 2.打开控制面板->程序-&g ...

  5. SQL 将列转成字符串并用逗号分隔

    SELECT STUFF((SELECT ',' + FieldName FROM TableName FOR XML PATH('')),1,1,'') AS T 其中的逗号可以换成其它字符 转换完 ...

  6. Redis核心解读(转)

    原文:Redis核心解读 Redis是知名的键值数据库,它广泛用于缓存系统.关于Redis的信息已经不用我多介绍了.这个系统的Redis文章主要从另外一个角度关注,Redis作为一个开源项目,短短2W ...

  7. WebService SOAP WSDL UDDI 使用php的curl、PHP5的SoapClient实现同步

    一.基本名词 WebService: WebService是一种跨编程语言和跨操作系统平台的远程调用技术.不同系统,不同语言的数据交换方法都是不同的,这就导致在不同系统,不同语言之间传递数据很麻烦,基 ...

  8. Python中的lambda、map和filter

    翻译.修改自https://medium.com/@happymishra66/lambda-map-and-filter-in-python-4935f248593 1.lambda lambda运 ...

  9. Python(递归)

    递归函数 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 举个例子,我们来计算阶乘n! = 1 x 2 x 3 x ... x n,用函数fact(n)表示,可以 ...

  10. exports与module.exports的区别

    nodejs有自己的模块系统,分为文件模块和内置模块.webpack是运行在node环境中,在学习vue-cli的webpack配置的时候, 发现有的文件模块: exports.fun1=functi ...