LeetCode(75) 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.
click to show follow up.
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 大小的元素,将其由小及大排序即可。
题目明确要求不可用标准库中的排序算法,尽可能使得算法高效。
我们常用的排序算法最优情况下也有 O(nlogn) 的时间复杂度,对待这样一个特殊的数组排序,我们应该采取其他更加高效的方法。
follow up 给出一种方法,分别计算元素0 , 1 , 2 的个数,然后对原数组重新赋值,简单高效,下面用该
方法给出程序实现。
AC代码
class Solution {
public:
void sortColors(vector<int>& nums) {
if (nums.empty())
return;
//初始化一个count数组,count[0] , count[1] , count[2] 分别记录nums中0 , 1 , 2出现个数
vector<int> count(3, 0);
vector<int>::iterator iter = nums.begin();
for (; iter != nums.end(); iter++)
{
if (*iter == 0)
count[0]++;
else if (*iter == 1)
count[1]++;
else if (*iter == 2)
count[2]++;
}//for
//对原数组排序
int i = 0;
while (i < count[0])
nums[i++] = 0;
while (i < (count[0] + count[1]))
nums[i++] = 1;
while (i < (count[0] + count[1] + count[2]))
nums[i++] = 2;
return;
}
};
LeetCode(75) Sort Colors的更多相关文章
- LeetCode(75):分类颜色
Medium! 题目描述: 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 ...
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
随机推荐
- Ubuntu开机之后报错结局方法
sudo gedit /etc/default/apport 把里面的enabled=1改成enabled=,保存 201. 就是下雨也去.202. 我马上拿来.203. 孙英开飞机.204. 国华来 ...
- nginx媒体压缩
1 gzip模块 参考:http://nginx.org/en/docs/http/ngx_http_gzip_module.html 浏览器的请求头里会表明Accept-Encoding 方式.服务 ...
- bzoj 5019 [Snoi2017]遗失的答案
题面 https://www.lydsy.com/JudgeOnline/problem.php?id=5019 题解 如果L不是G的倍数 答案为0 下面考虑G|L的情况 将G,L质因数分解 设$L= ...
- codeforces 615 D. Multipliers (数论 + 小费马定理 + 素数)
题目链接: codeforces 615 D. Multipliers 题目描述: 给出n个素数,这n个素数的乘积等于s,问p的所有因子相乘等于多少? 解题思路: 需要求出每一个素数的贡献值,设定在这 ...
- azkaban-web-start.sh启动时出现Table 'execution_flows' is marked as crashed and should be repaired Query错误的解决办法(图文详解)
问题详情 [hadoop@master bin]$ ./azkaban-web-start.sh Using Hadoop Using Hive from /home/hadoop/app/hive ...
- poj3252Round Numbers
链接 也算是组合 以前按组合做过一次 忘记怎么做的了 这次按dp写的 dp[i][j][g][k] 表示第i位为k(0|1)而且有j个1,g个0的情况数 貌似写的麻烦了...这一类的题,进行逐位计算就 ...
- AJPFX:不用递归巧妙求出1000的阶乘所有零和尾部零的个数
package com.jonkey.test; import java.math.BigInteger; public class Test6 { /*** @param args* 需求:求出1 ...
- Spring-bean(二)
命名空间 自动装配 bean之间的关系:继承:依赖 使用外部属性文件 SpEL bean的生命周期 bean的后置处理器 (一)util命名空间 当用list,set等集合时,不能将集合作为独立的be ...
- 【学习笔记】深入理解js原型和闭包(1)—— 一切都是对象
“一切都是对象”这句话的重点在于如何去理解“对象”这个概念. ——当然,也不是所有的都是对象,值类型就不是对象. 首先咱们还是先看看javascript中一个常用的运算符——typeof.typeof ...
- codevs 5438 zbd之难题(水题日常)
时间限制: 1 s 空间限制: 1000 KB 题目等级 : 白银 Silver 题目描述 Description zbd想要一个计算器,请你编一个计算器. 输入描述 Input Descrip ...