解法一:计数排序:统计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. css四可见,部分可见和重叠半透明

    <html> <head> <title>javascript</title> <style type="text/css"& ...

  2. ObjectMapper对象的使用 Object2JSON

    // // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler ...

  3. Python基础-5

    目录 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 hashlib re正则表达式 模块 ...

  4. UltraEdit 回车符替换空格

    查找和替换    输入 ^r^n   替换为:(空格)

  5. IOS 防坑指南

    1. 读写文件 1. IOS 8 中  stringWithContentsOfFile 已被移除 2. 创建文件必须放到 应用下 Documents 下面 // // FileHelper.swif ...

  6. NPOI 2.1.3.1导入Excel

    引入NPOI 2.1.3.1的包 项目引入 using NPOI.XSSF.UserModel;using NPOI.SS.UserModel; 控制器方法: public ActionResult ...

  7. WPF的Image控件图片不能显示出来的问题探究

    在wpf项目中,用Image来显示资源图片,在界面是可以显示,但是在运行的时候却显示不出来. <Image Source=" HorizontalAlignment="Lef ...

  8. 【leetcode 138. 复制带随机指针的链表】解题报告

    方法一:递归 unordered_map<Node*,Node*> dict; Node* copyRandomList(Node* head) { if (!head) return h ...

  9. Apple导出p12证书 导出证书为p12 Apple开发

    1.原因说明 p12证书包含了我们的cer证书和私钥 这个证书可以当做我们开发凭证的备份 在我们更换开发机器的时候不需要再去Apple开发中心申请了 2.导出过程 2.1 打开钥匙串访问 2.2 选择 ...

  10. 主流C语言编译器介绍