题目地址:Sort Colors - LeetCode


Given an array with n objects colored red, white or blue, sort them in-place 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.

Example:

Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]

Follow up:

  • A rather straight forward solution is a two-pass algorithm using counting sort.
    First, iterate the array counting number of 0’s, 1’s, and 2’s, then overwrite array with total number of 0’s, then 1’s and followed by 2’s.
  • Could you come up with a one-pass algorithm using only constant space?

这道题目很简单,就是排序

Python解法如下:

class Solution:def sortColors(self, nums: List[int]) -> None:"""Do not return anything, modify nums in-place instead."""if nums==[]:returna = [0, 0, 0]j = 0for i in nums:a[i] += 1for i in range(0, len(nums)):while a[j] <= 0:j += 1nums[i] = ja[j] -= 1

太简单了,其他解法就不写了。

LeetCode 75. Sort Colors--Python解法相关推荐

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

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

  2. 【排序】LeetCode 75. Sort Colors

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

  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

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

  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 | 75. 颜色分类(荷兰国旗问题,快速排序)

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

  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, ...

最新文章

  1. 搭建接口自动化测试环境,这3个工具就够了
  2. 批量ping工具fping
  3. python语言入门编程猫-编程猫推出海龟编程器,打造Python教育产品矩阵
  4. keras从入门到放弃 (一) 线性回归
  5. CRM BSP里控制左右对应对齐的属性align
  6. Pycharm2019版中打开.md .st文件出现卡死情况怎么办?
  7. Bitcoin 0.7.0 发布, P2P网络的匿名数字货币
  8. 爬虫之煎蛋网妹子图 大爬哦
  9. GBaseDataStudio管理工具
  10. 利用阿里云oss实现上传视频和图片功能
  11. cogs 2235 烤鸡翅
  12. 2010国家节假日安排
  13. 在.NET实现邮件收发功能(包含源代码)
  14. 【安全】Web指纹识别
  15. node爬取双马尾小姐姐图片
  16. 绿色IT十大攻略:营建企业绿色IT环境
  17. oracle utl_smtp,Oracle 11g 环境下,利用utl_smtp创建发送邮件的存储过程
  18. Spring+SpringMvc+Hibernate(SSH)+bootstrap/Jsp整合
  19. 图片滑块验证码的解决
  20. 浪迹天涯king教你用elementui做复杂的表格,去处理报表数据(合并表头,合并表体行和列)

热门文章

  1. 被“嫌弃”的AI药物设计
  2. Java中Math类常用函数总结
  3. ISME:根系招募特异型菌群增强植物对盐胁迫的抗性
  4. Microbiome:植物根际微生物组也有昼夜节律
  5. 水稻微生物组时间序列分析3-冲击图展示时间序序列变化
  6. linux看硬盘io,linux查看硬盘IO
  7. pandas使用groupby函数、first函数、last函数分别获得每个分组的第一行和最后一行数据(first/last row of each group in dataframe)
  8. pandas使用reindex函数为日期索引中有缺失日期的dataframe进行索引重置(所有日期都连续)、并使用fill_value参数为行进行默认填充
  9. seaborn使用jointplot函数为散点图添加边缘图、添加回归线、为边缘直方图添加密度曲线、自定义边缘直方图的色彩(Change Color of Marginal Histogram Plot
  10. R语言ggplot2可视化分面图(faceting):自定义分面图可视化、ggplot2可视化分面图并移除分面图之间的边框线条(Remove Panel Border Lines in a facet