要求

  • 给只有0 1 2三个元素的数组排序

思路

  • 方法1:遍历数组,利用辅助数组保存三个元素的个数,再写入(遍历两遍)

    • 辅助数组有三个元素,对应0 1 2的个数
  • 方法2:模拟三路快排,遍历一遍完成排序
    • 三个索引,zero和two用于首尾的扩充,i用于遍历
    • 遍历数组,每个元素只有三种可能
    • 0 2移到首尾,1不动

实现

方法1

  1. 1 void sortColors(vector<int>& nums){
  2. 2 int count[3] = {0};
  3. 3 for( int i = 0 ; i < nums.size() ; i ++ ){
  4. 4 assert( num[i] >= 0 && nums[i] <= 2 );
  5. 5 count[nums[i]] ++;
  6. 6 }
  7. 7
  8. 8 int index = 0;
  9. 9 for( int i = 0 ; i < count[0] ; i ++ ){
  10. 10 nums[index++] = 0;
  11. 11 for( int i = 0 ; i < count[1] ; i ++ ){
  12. 12 nums[index++] = 1;
  13. 13 for( int i = 0 ; i < count[2] ; i ++ ){
  14. 14 nums[index++] = 2;
  15. 15 }
  16. 16 }

方法2

  1. 1 void sortColors1(vector<int>& nums){
  2. 2 int zero = -1;
  3. 3 int two = nums.size();
  4. 4 for( int i = 0 ; i < two ; ){
  5. 5 if( nums[i] == 1 )
  6. 6 i ++;
  7. 7 else if( nums[i] == 2 ){
  8. 8 two --;
  9. 9 swap( nums[i] , nums[two] );
  10. 10 }else{
  11. 11 assert( nums[i] == 0 );
  12. 12 zero ++;
  13. 13 swap( nums[zero] , nums[i] );
  14. 14 i ++;
  15. 15 }
  16. 16 }
  17. 17 }

相关

215 Kth Largest Element in an Array

[刷题] 75 Sort Colors的更多相关文章

  1. 刷题75. Sort Colors

    一.题目说明 题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起.难度是Medium. 二.我的解答 这个是一个排序,还是 ...

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

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

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

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

  4. 75. Sort Colors - LeetCode

    Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...

  5. 【LeetCode】75. Sort Colors (3 solutions)

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

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

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

  7. 75. Sort Colors(中等)

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

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

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

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

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

随机推荐

  1. PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642 题目描述: Being unique is so important to peo ...

  2. 数据仓库系列之ETL中常见的增量抽取方式

    为了实现数据仓库中的更加高效的数据处理,今天和小黎子一起来探讨ETL系统中的增量抽取方式.增量抽取是数据仓库ETL(数据的抽取(extraction).转换(transformation)和装载(lo ...

  3. VIM 编辑器操作详解

    1 vim 使用介绍 1.1 vim 安装 # CentOS 安装: yum install -y vim # Ubuntu 安装: sudu apt-get install vim 安装完成后,可使 ...

  4. QT实现OPC_UA客户端程序以及与OPC_UA服务器通信

    1.OPC_UA服务器准备工作 1.关于OPC_UA服务器的搭建可以参考前面一篇文章:https://blog.csdn.net/xipengbozai/article/details/1150809 ...

  5. [矩阵乘法] PKU3233 Matrix Power Series

    [ 矩 阵 乘 法 ] M a t r i x P o w e r S e r i e s [矩阵乘法]Matrix Power Series [矩阵乘法]MatrixPowerSeries Desc ...

  6. 记一次metasploitable2内网渗透之21,22,23,25端口爆破

    Hydra是一款非常强大的暴力破解工具,它是由著名的黑客组织THC开发的一款开源暴力破解工具.Hydra是一个验证性质的工具,主要目的是:展示安全研究人员从远程获取一个系统认证权限. 目前该工具支持以 ...

  7. Vue CLI 是如何实现的 -- 终端命令行工具篇

    Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统,提供了终端命令行工具.零配置脚手架.插件体系.图形化管理界面等.本文暂且只分析项目初始化部分,也就是终端命令行工具的实现. 0. 用法 ...

  8. .netcore 写快递100的快递物流信息查询接口

    快递100的物流信息查询接口,官方提供了一些demo;还好官方提供的代码是.netcore版本写的,不过写的有点low;根据官方提供的代码,我按照.netcore 的风格重构了代码:核心代码如下: / ...

  9. Flowable中的Service

    前言 在学习博客[(https://blog.csdn.net/puhaiyang/article/details/79845248)]时,注意到Flowable中的各种Service(如下),进而在 ...

  10. 简析JAVA8函数式接口

    一,定义 "有且只有一个抽象方法的接口"----函数式接口的定义. @FunctionalInterface public interface Ifun{ void test(); ...