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 ...
随机推荐
- 转一篇关于vuex简单理解的文章
学习vuex半天摸不着头脑无意间发现了这篇文章 对vuex做了一个简单的阐述比较有助于我的理解 现在分享出来希望能给一些朋友一点帮助 这个是原文地址 http://www.ituring.com.c ...
- 解读邮箱正则表达式:^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
转自:http://www.cnblogs.com/joyceTING/archive/2013/05/09/3069089.html正则表达式 \w+([-+.]\w+)*@\w+([-.]\w+) ...
- Square roots
Loops are often used in programs that compute numerical results by starting with an approximate answ ...
- [学习笔记]HTTP协议
转自:www.cnblogs.com/li0803/archive/2008/11/03/1324746.html Author :Jeffrey 引言 HTTP是一个属于应用层的面向对象的协议,由于 ...
- BZOJ 3130 二分+网络流
思路: 不被题目忽悠就是思路 二分一下max 判一下等不等于最大流 搞定 7 9 1 1 2 3 1 3 3 2 3 3 3 4 2 3 5 2 3 6 1 4 7 2 5 7 2 6 7 2 这里有 ...
- 系统丢失的DLL文件问题根源解决(纯净官网下载放心)(图文详解)(博主推荐)
导言 最近,身边的朋友们,问我,他电脑的win10系统里 mfc110.dll 丢失. 其他的系统文件丢失修复,是一样的步骤. 现象 大家也许,都会有这么一个习惯,动不动则就去百度上搜索. 其实啊,这 ...
- VS10的一个问题
今天遇到一个问题,LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏.转一下网上的解决办法http://bbs.csdn.net/topics/390 ...
- spring-data-jpa 新增 修改 删除 查询 分页
1.查询所有数据 findAll() 2.分页查询 findAll(new PageRequest(0, 2)) 3.根据id查询 findOne() 4.根据实体类属性查询: findByPro ...
- JDBC连接SQL Server遇到的问题
需要使用到微软的JDBC sql server的驱动类,去官网下载jar包 使用的URL模式:"jdbc:sqlserver:地址:端口//;databaseName=YourDatabas ...
- Android组件Activity初探
1.Activity是什么 Activity是Android系统中的四大组件之一,在MVC模式中属于C控制层 M(Model 模型):Model是应用程序的主体对象. V(View 视图) ...