解法一:计数排序:统计0,1,2 的个数

时间复杂度:O(n)

空间复杂度:O(k)    k为元素的取值范围, 此题为O(1)

class Solution {
public:
void sortColors(vector<int>& nums) {
int count[] = {}; //存放0,1,2三个元素的频率
for(int i=;i<nums.size();i++){
assert(nums[i] >= && nums[i]<=); //若不符合条件则报错
count[nums[i]] ++;
}
int index = ;
for(int i=;i<count[];i++)
nums[index++] = ;
for(int i=;i<count[];i++)
nums[index++] = ;
for(int i=;i<count[];i++)
nums[index++] = ;
}
};

解法二:三路快排

时间复杂度:O(n)

空间复杂度:O(1)

只遍历了一遍

class Solution {
public:
void sortColors(vector<int>& nums) {
int zero = -; //nums[0...zero] == 0 , 即设置初始状态为无效的数组
int two = nums.size(); //nums[two...n-1] ==2, 初始化two==n
for(int i=;i<two;){
//有一部分i不需要++
if(nums[i] == )
i++;
else if(nums[i] ==){
two -- ; //two移位到前一位,即还没有处理的元素上 //将two前的还未排序的元素与2交换位置
swap(nums[i],nums[two]); }
else{
//nums[i] == 0
assert(nums[i] ==);
zero ++ ;
//zero++后指向的是1,相当于将1和0交换位置,所以i++
swap(nums[zero],nums[i]);
i++;
}
}
}
};

leetcode 75 Sort Colors 计数排序,三路快排的更多相关文章

  1. [LeetCode] 75. Sort Colors 颜色排序

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

  2. LeetCode 75. Sort Colors(排序颜色)

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

  3. leetCode 75.Sort Colors (颜色排序) 解题思路和方法

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

  4. LeetCode 75. Sort Colors (颜色分类):三路快排

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

  5. LeetCode 75. Sort Colors (python一次遍历,模拟三路快排)

    LeetCode 75. Sort Colors (python一次遍历,模拟三路快排) 题目分析: 本题需要实现数字只包含0,1,2的排序,并且要求一次遍历. 由于只用把数字隔离开,很容易想到快排的 ...

  6. 75. Sort Colors(颜色排序) from LeetCode

      75. Sort Colors   给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...

  7. 普林斯顿大学算法课 Algorithm Part I Week 3 重复元素排序 - 三路快排 Duplicate Keys

    很多时候排序是为了对数据进行归类,这种排序重复值特别多 通过年龄统计人口 删除邮件列表里的重复邮件 通过大学对求职者进行排序 若使用普通的快排对重复数据进行排序,会造成N^2复杂度,但是归并排序和三路 ...

  8. LeetCode 75 Sort Colors(颜色排序)

    翻译 给定一个包括红色.白色.蓝色这三个颜色对象的数组.对它们进行排序以使同样的颜色变成相邻的,其顺序是红色.白色.蓝色. 在这里,我们将使用数字0.1和2分别来代表红色.白色和蓝色. 原文 Give ...

  9. [leetcode]75. Sort Colors三色排序

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

随机推荐

  1. 基于PCL的屏幕选点、框选点云、单点选取

    1. 单点选取 #include <pcl/io/pcd_io.h> #include <pcl/point_cloud.h> #include <pcl/point_t ...

  2. jQuery基础教程-第8章-003Providing flexible method parameters

    一.The options object 1.增加阴影效果 (function($) { $.fn.shadow = function() { return this.each(function() ...

  3. jQuery--基础知识速查表

    一.jQuery选择器 选择器 实例 选取 * $("*") 所有元素 #id $("#lastname") id="lastname" 的 ...

  4. CF519E A and B and Lecture Rooms

    最近很颓……难题想不动……水题写不对,NOIP怕是

  5. ubuntu安装软件包apt-get和dpkg方法

    1apt方法 安装软件 apt-get install softname1 softname2 softname3…… 卸载软件 apt-get remove softname1 softname2 ...

  6. Django-restframework25 Pagination(分页)

    Django-restframework25 Pagination(分页) 2017年11月11日 15:14:36 敲代码的伪文青 阅读数:1021 标签: restful 更多 个人分类: res ...

  7. 关于instanceof测试遇到的问题

    今天上上课 用 instanceof关键字来判断某个对象是否属于某种数据类型.报错  代码如下 package cn.lijun.demo3; import cn.lijun.demo.Person; ...

  8. Xcode弱网测试工具

    Network Link Conditioner Network Link Conditioner工具是Mac下提供的一个弱网测试工具. 安装Network Link Conditioner Xcod ...

  9. Java IO输入输出流 FileWriter 字符流

    字节缓冲流 //为什么要使用包装流,使用包装流是为了提高读写操作的性能. public class Packing_flowDemo { public static void main(String[ ...

  10. JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(1):Mybatis和Hibernate概念理解

    一.关键字说明: oop:面向对象 aop:面向切面 ioc:控制反转 orm:对象关系映射 pojo:数据库表映射的java实体类 二.常识说明:1.hibernate和mybatis都属于持久层. ...