题目描述:

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
    Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
    Note: you are not suppose to use the library's sort function for this problem.

分析:
    题意:给定n个有红、白、蓝三种颜色的物体,把它们按照红、白、蓝的颜色排序。在整型数组中,用0、1、2分别表示红、白、蓝三种颜色。不能使用排序函数。
    思路:这是一道经典的三颜色排序问题,可以采用双指针法解决。我们初始化指针red = 0、white = 1、blue = 2。Ⅰ. 如果white < blue且指针white指向2(蓝色),应该把蓝色换到后面去,即交换指针white和指针blue指向的值,同时blue减一;Ⅱ. 如果white > red且指针white指向0(红色),应该把红色换到前面去,即交换指针white和指针red指向的值,同时red加一。
    时间复杂度为O(n),空间复杂度为O(1)。
    这里给出两个版本代码(思路总体一致,第二个版本更优雅)。

代码:

#include <bits/stdc++.h>using namespace std;// T(n) = O(n)
// Accepted
class Solution {
public:void sortColors(vector<int>& nums) {int n = nums.size();// Exceptional Case: if(n == 0){return;}// red, white, blueint red = 0, white = 0, blue = n - 1;// for case: [1, 0], white < blue will cause an error!while(white <= blue){if(nums[white] == 1){white++;}else if(nums[white] == 0){if(nums[red] != 0){swap(nums[red], nums[white]);}red++;white++;}else if(nums[white] == 2){if(nums[blue] != 2){swap(nums[white], nums[blue]);}blue--;}}}
};

代码:

#include <bits/stdc++.h>using namespace std;// T(n) = O(n)
class Solution {
public:void sortColors(vector<int>& nums) {int n = nums.size();// Exceptional Case: if(n == 0){return;}int red = 0, blue = n - 1;for(int white = red; white <= blue; white++){while(nums[white] == 2 && white < blue){swap(nums[white], nums[blue--]);}while(nums[white] == 0 && white > red){swap(nums[red++], nums[white]);}}}
};

拓展:给定n个有k种颜色的物体,把它们按照k种颜色排序。在整型数组中,用0、1、2、...、k - 1分别表示k种颜色。不能使用排序函数。
    思路:这道题是三颜色排序的进化版本,k颜色排序题,总体还是采用双指针法,重复部分不再复述。假设当前排序考虑的颜色范围是[c_min,c_max],则它们初始值为c_min = 0、c_max = k - 1。一轮排序之后,两种颜色c_min = 0、c_max = k - 1已经处理完毕(分别交换至数组两端),接下来考虑[c_min + 1 = 1,c_max - 1 = k - 2]范围内的颜色排序,并重复该步骤直到c_min >= c_max,退出该循环。
    时间复杂度为O(n * k)。

代码:

#include <bits/stdc++.h>using namespace std;class Solution{
public:void sortColorsII(vector<int>& nums, int k){int n = nums.size();// exceptional case: if(n <= 1 || k <= 1){return;}int c_min = 0, c_max = k - 1;int left = 0, right = n - 1;int i = 0;while(c_min < c_max){while(i <= right){if(nums[i] == c_min){swap(nums[left++], nums[i++]);}                                else if(nums[i] == c_max){swap(nums[i], nums[right--]);}else{i++;}}i = left;c_min++;c_max--;}}
};

LeetCode 75. Sort Colors(三颜色排序→K颜色排序)相关推荐

  1. 【排序】LeetCode 75. Sort Colors

    LeetCode 75. Sort Colors Solution1: 参考自:<leetcode-cpp 答案> 由于0,1,2非常紧凑,首先想到计数排序(counting sort), ...

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

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

  3. [LeetCode]75.Sort Colors

    [题目连接] 75.Sort Colors [题目] Given an array with n objects colored red, white or blue, sort them so th ...

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

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

  5. leetcode 75. Sort Colors | 75. 颜色分类(荷兰国旗问题,快速排序)

    题目 https://leetcode.com/problems/sort-colors/ 题解 快速排序3.0(随机快排+荷兰国旗技巧优化) 在arr[L-R]范围上,进行快速排序的过程: 1)在这 ...

  6. LeetCode 75. Sort Colors

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

  7. 个人记录-LeetCode 75. Sort Colors

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

  8. 75. Sort Colors - LeetCode

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

  9. Leetcode 075 Sort Colors

    题目连接:Leetcode 075 Sort Colors 解题思路:从左向右遍历,同时维护两个指针r和b,0~r位置上的元素为0,b~n-1位置上的为2.在遍历过程中,碰到0,放到r的位置:碰到2, ...

  10. LeetCode 75. Sort Colors--Python解法

    题目地址:Sort Colors - LeetCode Given an array with n objects colored red, white or blue, sort them in-p ...

最新文章

  1. 一起ORACLE数据库中数据查询结果不一致问题的排查过程
  2. 机器学习笔记(二)逻辑回归和正则化
  3. 功利性学习:别陷入勤奋陷阱
  4. git merge与rebase
  5. (王道408考研数据结构)第七章查找-第四节:哈希表(基本概念及其操作)
  6. 一文搞清楚 Spark shuffle 调优
  7. Asp.Net Core基于JWT认证的数据接口网关Demo
  8. logback之使用demo
  9. 扬州大学广陵学院c语言试卷,扬州大学广陵学院交直流调速复习题答案(试卷)
  10. ORCAD学习系列之一 ORCAD元器件库的建立
  11. android移动应用技术,Android移动开发技术与应用.pdf
  12. 前端性能优化方法总结
  13. JAVA多线程是什么
  14. 国产操作系统--NeoKylin基本操作命令汇总(一)
  15. 关于梯度下降与Momentum通俗易懂的解释
  16. 箱形图适用于哪种数据_数据可视化分析中图表选择
  17. 《回炉重造》——注解
  18. html圆形头像简易实现
  19. Oracle入门精读14_Lsnrctl命令
  20. DBA必知的170张Oracle常用动态性能表介绍

热门文章

  1. pycharm 文件名不同颜色所代表的含义
  2. 那些年,我们一起卸载过的软件…
  3. STM32F103C8T6最小系统板原理图+PCB文件
  4. 五险一金,这篇就够了
  5. PS全套抠图技法教程,快速抠头发/抠婚纱,带配套素材
  6. JAVA代码重复率多少达标_【案例】代码重复率太高不要怕,求真老师教你化险为夷!...
  7. VLAN-TAG 知识相关
  8. 电脑服务器地址能修改吗,电脑ip地址可以随便改吗
  9. 浅析Minecraft直播弹幕模组BakaDanmaku源码
  10. 生活随记 - 方便面