Sort Colors题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/sort-colors/description/


Description

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.

Solution

void sortColors(int* nums, int numsSize) {
int b[3] = {0};
int i;
for (i = 0; i < numsSize; i++)
b[nums[i]]++;
for (i = 0; i < b[0]; i++)
nums[i] = 0;
for (i = b[0]; i < b[0] + b[1]; i++)
nums[i] = 1;
for (i = b[0] + b[1]; i < numsSize; i++)
nums[i] = 2;
}

解题描述

这道题一上手想到的解法就是桶排序。因为数字的范围是确定的且较小。但是跑出来的时间挺长的。尝试手写快排看看能不能提速,发现还是差不多的,所以Solution只给出了写起来容易点的桶排序。

[Leetcode Week2]Sort Colors的更多相关文章

  1. 【LeetCode】Sort Colors 数组排序

    题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...

  2. LeetCode 75. Sort Colors (颜色分类):三路快排

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

  3. 【LeetCode】Sort Colors

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

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

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

  5. [LeetCode题解]: Sort Colors

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given an a ...

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

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

  7. Leetcode 75. Sort Colors

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

  8. 【leetcode】Sort Colors(middle)☆

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

  9. Java for LeetCode 075 Sort Colors

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

随机推荐

  1. 8.0 TochAction各种用法

    1.滑动---TouchAction  支持相对坐标.绝对坐标.Element 注意看顶部的导入TouchAction这个库.. #实例化 action = TouchAction(driver) # ...

  2. 学习人工智能的第五个月[字典学习[Dictionary Learning,DL]]

    摘要: 大白话解释字典学习,分享第五个月的学习过程,人生感悟,最后是自问自答. 目录: 1.字典学习(Dictionary Learning,DL) 2.学习过程 3.自问自答 内容: 1.字典学习( ...

  3. windowsserver2008 重新启动计算机命令

    在windowsserver2008中,若要重新启动计算机,可以输入以下命令即可立即重启计算机shutdown -r -t 0命令意义:shutdown在英文中意为关掉,在计算机中即为关机参数意义:- ...

  4. 预处理器&预处理变量&头文件保护&条件编译

    [常见的预处理功能] #include 头文件保护符 条件编译 [预处理器] 编译之前执行的一段程序,可以部分地改变我们所写的程序 举个例子:当预处理器看到#include标记时就会用指定的头文件的内 ...

  5. JAVA高并发处理------多线程

    线程安全概念:当多个线程访问某一个类(对象或方法)时,这个对象始终都能表现出正确的行为,那么这个类(对象或方法)就是线程安全的. 分析:当多个线程访问myThread的run方法时,以排队的方式进行处 ...

  6. Contest Hunter 模拟赛09 C [树形dp+差分]

    题面 传送门 思路 又双叒叕是一道差分题我没想出来......记录一下 首先这个"所有祖先都比自己小"等价于"父亲比自己小" 这题的基础dp方程很显然,$dp[ ...

  7. 【BZOJ4325】NOIP2015 斗地主 搜索+贪心

    这个东西考试的时候一眼以为状压就压炸了考试又了一下午.....最后我打出来发现后几个点10min都过不去,我大概算了一下,可能是吧.......最后一脸懵逼的我去怂了正解,我们发现只要确定了顺子就可以 ...

  8. BZOJ 3262: 陌上花开 CDQ

    这个题大部分人用了离散然后水之,然而.....作为一只蒟蒻我并没有想到离散,而是直接拿两个区间一个对应n,一个对应k来搞,当然这两个区间是对应的,我把第一维排序,第二维CDQ,第三维树状数组,然而由于 ...

  9. watch用法小记

    By francis_hao    Jun 30,2017   watch:周期性的执行一个一个程序,并全屏显示输出 概述 watch [options] command   描述 watch重复的运 ...

  10. SCOI2008奖励关 [状压dp]

    题目描述 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出k次宝物,每次你都可以选择吃或者不吃(必须在抛出下一个宝物之前做出选择,且现在决定不吃的宝物以后也不能再 ...