leetcode笔记:Sort Colors
一. 题目描写叙述
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.
Follow up:
A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0’s, 1’s, and 2’s, then overwrite array with total number of 0’s,
then 1’s and followed by 2’s.
Could you come up with an one-pass algorithm using only constant space?
二. 题目分析
题目一開始说到一组对象,包括红白蓝三种颜色。然后要对他们进行排序,说白了就是对一个仅仅含有0, 1, 2三个数字的数组从小到大排序。
题目要求:come up with an one-pass algorithm using only constant space,因此仅仅能扫描一次。这里採取的方法是,统计0, 1, 2三个数字分别出现的次数。再将数组nums又一次构建为从0到2排列的数组,这样的方法没有使用数组元素间的交换,仅仅需扫描一次nums。
三. 演示样例代码
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
void sortColors(vector<int>& nums)
{
int SIZE = nums.size();
int count[3] = {0, 0, 0};
for (int i = 0; i < SIZE; ++i)
++count[nums[i]];
for (int i = 0, index = 0; i < 3; ++i)
for (int j = 0; j < count[i]; ++j)
nums[index++] = i;
}
};
一个測试结果:
四. 小结
本题的解法还是挺多的,可多參考网上的其它解法。
leetcode笔记:Sort Colors的更多相关文章
- [Leetcode Week2]Sort Colors
Sort Colors题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/sort-colors/description/ Description Give ...
- 【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 ...
随机推荐
- What are the differences between WebAPI and WebAPI 2
http://stackoverflow.com/questions/21298961/what-are-the-differences-between-webapi-and-webapi-2 Maj ...
- jni java与c++交互返回三维数组jobjectArray
1.在java里创建一个类,在类里添加静态方法调用c++的类库dll,在java里创建要返回数组的函数.在java的main方法里创建返回后的测试方法. package cn.gov.cma.cnn. ...
- PowerDesigner怎么调出工具箱?
PowerDesigner怎么调出工具箱? 如图操作:
- 安装vue时使用npm install 报错
npm ERR! Darwin 14.3.0 npm ERR! argv "/usr/local/Cellar/node/6.4.0/bin/node" "/usr/lo ...
- hdu 1022 - 数据结构 栈
题目链接 按序列a进栈,问能不能按序列b出栈. 遍历b,如果当前元素进过栈了,那么必须和栈顶元素相同.如果没进过栈则按a序列压栈,直到遇到当前元素. #include <iostream> ...
- UI Framework-1: Aura Client API
Client API The Aura Client API is an API Aura uses to communicate with the client application using ...
- 为什么 linux 上不能用 localhost 链接数据库?
因为 linux 连接的时候不是通过 tcp 协议,而是通过 sockect 来连接.所以 写localhost 之后就会默认去找 sockect 链接[此文件在 /var/lib/mysq ...
- docker切换默认镜像源
docker切换默认镜像源 基于 debian8 默认安装的 docker 镜像源是在国外,pull 镜像的时候奇慢无比,需要自己手动切换成国内的镜像源. 1. 修改配置文件 docker 默认的 ...
- nodejs学习(一)--express+ejs生成项目
一.说一下 首先保证node环境已经安装完毕,npm能正常使用,安装可自行百度,比较简单,在此不做赘述. 二.直接开码 安装 npm install express express-generator ...
- HTTP状态码以及其含义大全 _IT技术小趣屋
原文:HTTP状态码以及其含义大全 _IT技术小趣屋 HTTP状态码(英语:HTTP Status Code)是用以表示网页服务器超文本传输协议响应状态的3位数字代码.我们在开发过程中比较常见的状态码 ...