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. Leetcode代码补全——二叉树

    在刷leetcode的过程中发现,在原网页输入答案是不需要自己构筑树和链表的,虽然便于直接思考算法,但是久而久之类似过于依赖编辑器,反而不知道如何创建树和链表,因此总结了该网页省略的部分,以其中题为例 ...

  2. 多文件上传 input 的multiple 属性

    一.上传多张图片并且预览 HTML: <div class="container"> <label>请选择一个图像文件:</label> < ...

  3. NOIP 2009 靶形数独(DLX)

    小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向Z 博士请教,Z 博士拿出了他最近发明的“靶形数独”,作 ...

  4. vue2.0中父子组件之间的通信总结

    父组件: 子组件: 接受父组件的信息: 向父组件发送事件: (其中slot是插槽,可以将父组件中的<p>123</p>插入进来,如果父组件没有插入的内容,则显示slot内部的内 ...

  5. 【Python】Python中子类怎样调用父类方法

    python中类的初始化方法是__init__(),因此父类子类的初始化方法都是这个,如果子类不实现这个函数,初始化时调用父类的初始化函数,如果子类实现这个函数,就覆盖了父类的这个函数,既然继承父类, ...

  6. 【bzoj1465/bzoj1045】糖果传递 数论

    题目描述 老师准备了一堆糖果, 恰好n个小朋友可以分到数目一样多的糖果. 老师要n个小朋友去拿糖果, 然后围着圆桌坐好, 第1个小朋友的左边是第n个小朋友, 其他第i个小朋友左边是第i-1个小朋友. ...

  7. javascript中window.location.search方法简介

    window.location.search方法是截取当前url中"?"后面的字符串,示例如下: 例如:http://www.firefoxchina.cn/?ntab截取后的字符 ...

  8. [NOI2017]游戏 2-sat

    ---题面--- 题解: 首先观察到,如果没有x的话,这就是一个2-sat问题. 建图方式:对于限制d1 c1 d2 c2,其中d1, d2分别代表比赛编号,c1, c2代表出场的赛车. 1,如果d1 ...

  9. [Leetcode] Path Sum路径和

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  10. [WC2007]剪刀石头布——费用流

    比较有思维含量的一道题 题意:给混合完全图定向(定向为竞赛图)使得有最多的三元环 三元环条件要求比较高,还不容易分开处理. 正难则反 考虑,什么情况下,三元组不是三元环 一定是一个点有2个入度,一个点 ...