题意:

  一个数组只可能含有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. git的一个merge流程

    git merge testSupport 合并testSupport分支代码到当前分支. 若无冲突发生,git commit -m "RM ID:5094",在git push即 ...

  2. [转]Web基础架构:负载均衡和LVS

    以下内容转载自:http://www.importnew.com/11229.html 在大规模互联网应用中,负载均衡设备是必不可少的一个节点,源于互联网应用的高并发和大流量的冲击压力,我们通常会在服 ...

  3. windows下捕获dump

         一般要捕获异常只需要两个函数:SetUnhandledExceptionFilter截获异常:MiniDumpWriteDump写dump文件.但是由于CRT函数可能会在内部调用SetUnh ...

  4. [转]BeginInvoke和EndInvoke方法浅析

    开发语言:C#3.0   IDE:Visual Studio 2008   一.C#线程概述   在操作系统中一个进程至少要包含一个线程,然后,在某些时候需要在同一个进程中同时执行多项任务,或是为了提 ...

  5. 使用HTTP访问网络------使用HTTPURLConnection

    HTTPURLConnection继承了URLConnection,因此也可用于向指定网站发送GET请求.POST请求.它在URLConnection的基础上提供了如下便捷的方法: 1.int  ge ...

  6. Asynchttpclient开源框架下载图片和文本,于Volley和Glide开源框架的区别。

    AsyncHttpClient是一款比较流行的Android异步网路加载库,在github上的网址是:https://github.com/loopj/android-async-httpAsyncH ...

  7. 如何登录Google美国服务器

    Google访问须知: ① 先访问一次 https://www.google.com/ncr ,禁止“国家重定向(No country Redirect) ” ② 再点击右上角齿轮图标,选第一项“Se ...

  8. C语言实例代码

    绘制余弦曲线和直线 #include #include int main() { double y; int x,m,n,yy; for(yy=0;yy<=20;yy++) {y=0.1*yy; ...

  9. JavaScript基础--超级玛丽(七)(上下左右控制)

    相信大家都玩过超级玛丽,下面实现控制玛丽的上.下.左.右等基本功能,本篇只是在练习JavaScript的用法 1.创建一个HTML页面 <!doctype html> <html l ...

  10. ios category类别的使用

    ios category类别的使用 Objective-C提供了一个非常灵活的类(Class)扩展机制-类别(Category).类别用于对一个已经存在的类添加方法(Methods).你只需要知道这个 ...