题意:

  一个数组只可能含有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. BZOJ3308 九月的咖啡店

    Orz PoPoQQQ 话说这题还有要注意的地方... 就是...不能加SLF优化,千万不能加 n = 40000,不加本机跑出来2sec,加了跑出来40sec...[给跪了 /*********** ...

  2. oracle表分区【转】

          摘要:在大量业务数据处理的项目中,可以考虑使用分区表来提高应用系统的性能并方便数据管理,本文详细介绍了分区表的使用. 在大型的企业应用或企业级的数据库应用中,要处理的数据量通常可以达到几十 ...

  3. 在Web中如何使用Windows控件(ActiveX)[转]

    最近做的一个Web项目,需要在网页中播放摄像头采集的实时视频,我们已经有了播放视频的使用C#编写的windows控件,如何将其嵌入到网页中去了?这需要使用一种古老的技术,ActiveX. 1.将.Ne ...

  4. HtmlAgilityPack解析全国区号页面到XML

    需求:完成一个城市和区号的xml配置文件 处理思路:通过HtmlAgilityPack解析一个区号页面,生产xml文件 页面:http://www.hljboli.gov.cn/html/code.h ...

  5. WCF服务编程中使用SvcMap实现类型共享等技巧【转】

    原文链接:http://www.cr173.com/html/19026_1.html 国外的一篇文章:Sharing DataContracts between WCF Services 文中提到的 ...

  6. java反射之Class.getMethod与getDeclaredMethods()区别

    Class对象的getMethods和getDeclaredMethods都是获取类对象的方法.但是又有所不同.废话不多说, 先看demo package com.westward; public c ...

  7. POJ 2886 Who Gets the Most Candies?

    思路: 对于 k 位置的 孩子,他的 数字是 +num 那么因为他自己本身是要被踢走的,所以相对位置 为k= k+num-1 如果数字是 -num,那么按正着数就没影响,k=k-num.线段树存储当前 ...

  8. Countries in War (POJ 3114) Tarjan缩点+最短路

    题目大意: 在一个有向图中,每两点间通信需要一定的时间,但同一个强连通分量里传递信息不用时间,给两点u,v求他们最小的通信时间.   解题过程: 1.首先把强连通分量缩点,然后遍历每一条边来更新两个强 ...

  9. 3.精通前端系列技术之深入学习Jquery(一)

    使用Jquery的好处: •简化JS的复杂操作 •不再需要关心兼容性(原生js获取元素样式.事件需要做兼容性) •提供大量实用方法 1.选择网页元素 <!DOCTYPE html PUBLIC ...

  10. Linux-如何查看登陆shell的类型

    输入一个系统不认识的命令(如#ig)获得系统提示 aix/#ig ksh ig not found #echo $ (适用sh/ksh) aix/#echo $ ksh #echo $SHELL(用户 ...