题意:

  一个数组只可能含有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 (技巧)的更多相关文章

  1. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  2. [LeetCode] Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  3. [leetcode]Sort Colors @ Python

    原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, wh ...

  4. [Leetcode] Sort Colors (C++)

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  5. 75.[LeetCode] Sort Colors

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  6. [LeetCode] Sort Colors 对于元素取值有限的数组,只遍历一遍的排序方法

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  7. [LeetCode] Sort Colors 只有3个类型的排序

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  8. 【LeetCode】Sort Colors 数组排序

    题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...

  9. 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 ...

随机推荐

  1. mvc+ef中比较数据

    例如:根据Para表中的type和paraid 字段进行比较 public class TypeComparer : IEqualityComparer<Para> { bool IEqu ...

  2. qml json 解析到 ListView

    https://github.com/kromain/qml-utils/tree/master/JSONListModel 非常棒!! 实现的原理如下文: http://goessner.net/a ...

  3. Servlet作业--实现注册和登录

    1.注册页面  zhuce.html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " ...

  4. c实现的iOS http下载类。支持自己设定http 头(比如cookie等)

    也许有人要问为什么要自己用c写? 原因是: 1 我是菜鸟 2 我最最初选择了AsyncSocket,弄了很久,基本上稳定了,但有时候出现了数据不完整,但我又没办法在这个时候识别到并重试:所以不完美,最 ...

  5. POJ 3299 Humidex 难度:0

    题目链接:http://poj.org/problem?id=3299 #include <iostream> #include <iomanip> using namespa ...

  6. 二模 (2) day2

    第一题: 题目描述: 在一个长方形框子里,最多有 N(0≤N≤6)个相异的点.在其中任何-个点上放一个很小的油滴,那么这个油滴会一直扩展,直到接触到其他油滴或者框子的边界.必须等一个油滴扩展完毕才能放 ...

  7. Prime Palindromes

    题目大意:求出区间[a,b]之间的回文质数. a<=b<=10^8; 解题过程: 1.先打个素数表,新学了个 欧拉筛法,是对普通筛法的改进.普通筛法是每找到一个素数,就把它的所有倍数标记成 ...

  8. UVA 10970-Big Chocolate

    题目: 给你一块M*N的巧克力,问把它切成最小单元需要最少切几刀,分开的就不能一起切了. 分析: 每次切割只能多产生一个部分,分成M*N个部分,必然要切M*N-1刀. 一个长为m宽为n的长方形和m*n ...

  9. MYSQL数据库导入导出(可以跨平台)

    MYSQL数据库导入导出.sql文件 转载地址:http://www.cnblogs.com/cnkenny/archive/2009/04/22/1441297.html 本人总结:直接复制数据库, ...

  10. Line计划今年全面进军中国市场:建立本地团队

    北京时间6月13日下午消息,<华尔街日报>报道称,移动消息应用Line计划于今年晚些时候进军中国市场.Line将在中国建立本地团队,开发内容和功能,从而进一步开拓中国这一全球最大的移动市场 ...