[Leetcode Week2]Sort Colors
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的更多相关文章
- 【LeetCode】Sort Colors 数组排序
题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,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 ...
- 【LeetCode】Sort Colors
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 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
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given an a ...
- [LeetCode] 75. Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- 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(middle)☆
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 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 ...
随机推荐
- Ubuntu下使用Git_6
这回真的是最后一篇了,哈哈,改写提交. 这里这部分在目前的学习阶段还没有用到,所以,这里将不在有实验的部分,在下面的链接中有详细的介绍 这也是我第一讲一个网站的内容完成的学习完成,这这部分,我讲简单的 ...
- Asp.net core中Migration工具使用的交流分享
a,ul>li,em{ color:deeppink !important; } h2>a{ text-decoration:none; } ul>li{ padding:3px; ...
- cut 与 awk
cut和awk都能分割显示需要的内容 但在需要以空格为分隔符的情况下: # free -m|grep Mem Mem: cut是以单一空格为分隔符的: # free -m|grep Mem|cut - ...
- Go基础篇【第4篇】: 内置库模块 bufio
bufio包实现了有缓冲的I/O.它包装一个io.Reader或io.Writer接口对象,创建另一个也实现了该接口,且同时还提供了缓冲和一些文本I/O的帮助函数的对象. 即:为了解决CPU与磁盘IO ...
- CentOS7中rpm,yum软件安装命令
RPM包常用安装位置说明 /etc/ 配置文件安装目录 /usr/bin/ 可执行的命令安装目录 /usr/lib/ ...
- Xshell出现要继续使用此程序必须应用到最新的更新或使用新版本
资源可以用,但是安装完成后启动会报错:“要继续使用此程序,您必须应用最新的更新或使用新版本” 解决办法先修改你电脑时间为前一年(2017 1月),然后就可以打开xshell了,打开后"工具& ...
- LevelDB速记
LevelDb的基本结构如下: 由六大部分组成: 一.MemTable,用户写入和读取的直接对象, 二.Immutable MemTable,用户状态写入的对象写满的MemTable之后会转为Immu ...
- sql in()批量操作
//批量修改 update 表A set A.name='n' where A.id in(字符串); //批量删除 delete from 表名称 where 列名称 ...
- [洛谷P2568]GCD
题目大意:给你$n(1\leqslant n\leqslant 10^7)$,求$\displaystyle\sum\limits_{x=1}^n\displaystyle\sum\limits_{y ...
- [AHOI2013]作业 & Gty的二逼妹子序列 莫队
---题面--- 题解: 题目要求统计一个区间内数值在[a, b]内的数的个数和种数,而这个是可以用树状数组统计出来的,所以可以考虑莫队. 考虑区间[l, r]转移到[l, r + 1],那么对于维护 ...