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 ...
随机推荐
- AtCoder Grand Contest 016 B - Colorful Hats
题目传送门:https://agc016.contest.atcoder.jp/tasks/agc016_b 题目大意: 有\(N\)只猫,每只猫头上带着一个帽子,帽子有颜色,现在告诉你每只猫能看到的 ...
- Query on a tree again! SPOJ - QTREE3
https://vjudge.net/problem/SPOJ-QTREE3 https://www.luogu.org/problemnew/show/P4116 一个log(LCT)比两个log( ...
- Codeforces Round #323 (Div. 2)
被进爷坑了,第二天的比赛改到了12点 水 A - Asphalting Roads /************************************************ * Author ...
- 学习JavaScript数据结构与算法 (一)
学习JavaScript数据结构与算法 的笔记, 包含一二三章 01基础 循环 斐波那契数列 var fibonaci = [1,1] for (var i = 2; i< 20;i++) { ...
- FACVSPOW - Factorial vs Power 数学方法 + 二分
http://www.spoj.com/problems/FACVSPOW/ 求解n! > a^n最小的整数n 对于有n!和a^n的东西,一般是取ln 然后就是求解 (ln(1) + ln(2) ...
- JSP九大内置对象的作用和用法总结【转】
JSP九大内置对象的作用和用法总结? JSP中一共预先定义了9个这样的对象,分别为:request.response.session.application.out.pagecontext.c ...
- 动手实现 Redux(一):优雅地修改共享状态
从这节起我们开始学习 Redux,一种新型的前端“架构模式”.经常和 React.js 一并提出,你要用 React.js 基本都要伴随着 Redux 和 React.js 结合的库 React-re ...
- C#局部类型partial在定义实体类Model中的应用
以前一直用继承类的方法,原来还可以这样 //例如:定义一个Person的实体类,用户ID(PersonId),姓名(Name),性别(Sex),年龄(Age),地址(Address),联系方式(Tel ...
- AJPFX关于java中可访问控制符和非访问控制符的详细总结
1.类的修饰符分为:可访问控制符和非访问控制符两种. 可访问控制符是:公共类修饰符 public 非访问控制符有:抽象类修饰符 abstract :最终类修饰符 final 1 )公共类修饰符 pub ...
- 【js数据结构】图的深度优先搜索与广度优先搜索
图类的构建 function Graph(v) {this.vertices = v;this.edges = 0;this.adj = []; for (var i = 0; i < this ...