LeetCode Sort Colors (技巧)
题意:
一个数组只可能含有3种数据,分别为1,2,3,请将数组排序(只能扫一遍)。
思路:
如果扫两遍的话,用个桶记录一下,再赋值上去就行了。
class Solution {
public:
void sortColors(vector<int>& nums) {
int cnt[]={};
for(int i=; i<nums.size(); i++)
cnt[nums[i]]++;
for(int j=,i=; j<; j++)
while(cnt[j]-- > )
nums[i++]=j;
}
};
AC代码
还有这种傻瓜方法,扫一遍先将2移到末尾,再扫一遍将0移到前面。仍然无效率。
class Solution {
public:
void sortColors(vector<int>& nums) {
int L=, R=nums.size()-;
for(int i=; i<nums.size(); i++)
{
while(L<R && nums[L]!=) L++;
while(L<R && nums[R]==) R--;
if(L<R) swap(nums[L],nums[R]);
}
L=, R=nums.size()-;
for(int i=; i<nums.size(); i++)
{
while(L<R && nums[L]!=) L++;
while(L<R && nums[R]!=) R--;
if(L<R) swap(nums[L],nums[R]);
}
}
};
AC代码
还有一种吊吊的,扫一遍就搞定的。扫一遍数组,考虑nums[i],如果nums[i]=2,立刻换到末尾,此时nums[i]有可能仍然是2,如果是2,一直继续换到末尾。这样就保证了区间(L,i)中不可能出现2,如果此时nums[i]为0,就与前面的换,此时nums[i]就只可能是0或1了,0就一直换,1就pass。
class Solution {
public:
void sortColors(vector<int>& nums) {
int L=-, R=nums.size();
for(int i=; i<R; i++)
{
while(i<R&&nums[i]==)
swap(nums[i],nums[--R]);
if(nums[i]==)
swap(nums[i],nums[++L]);
}
}
};
AC代码
LeetCode Sort Colors (技巧)的更多相关文章
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- [LeetCode] 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 @ Python
原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, wh ...
- [Leetcode] Sort Colors (C++)
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 75.[LeetCode] 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 对于元素取值有限的数组,只遍历一遍的排序方法
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [LeetCode] Sort Colors 只有3个类型的排序
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 【LeetCode】Sort Colors 数组排序
题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...
- LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors
1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...
随机推荐
- Play Framework介绍:主要概念(转)
Play Framework是一个Rails风格的Full-stack Java Web框架. MVC模型 Play应用遵循Web架构使用的MVC架构模式. 它将应用分离到不同的层中:表现层(Pres ...
- mvc学习记录
1.关于mvc中的session在controller中传递 在用mvc开发新项目的时候,不久就遇到一个头大的问题,session在controller中传递居然出现空值,但明明一开始就赋值了,通过度 ...
- ANGULAR JS PROMISE使用
Promise是一种模式,以同步操作的流程形式来操作异步事件,避免了层层嵌套,可以链式操作异步事件. 我们知道,在编写javascript异步代码时,callback是最最简单的机制,可是用这种机制的 ...
- table表格
表格是一种组织整理的数据的手段,在div布局还未流行是,也用来布局,一个表格包含了表格整体.表格头部.每个表格均有若干行,每行被分为若干单元格. 在HTML中表格使用table标签来定义,行由< ...
- POJ 1979 Red and Black dfs 难度:0
http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...
- 内工大acm校赛--整理代码
题目:小明搜到一行无缩进无换行代码,请帮小明整理代码.无for语句和case语句,而且只有一个主函数.你只要控制注意“:”“{”“}”这三个符号带来的缩进和换行效果就行. Input: 输入只有一行, ...
- (DFS)hdoj1010-Tempter of the Bone
#include<cstdio> #include<cmath> #include<stdlib.h> ][]={{,},{,-},{,},{-,}},escape ...
- Python中的sorted函数以及operator.itemgetter函数 【转载】
operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. a = [1,2 ...
- iphone判断当前网络连接类型
eachability只能区分出无网络.wifi和wwan(2G&2.5G&3G)类型的网络连接类型,只需重构networkStatusForFlags方法,即可详细区分出2G与3G网 ...
- 学习js之类的使用
<script language="javascript">function Person(){ }Person.prototype={ name:null ...