题目

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.

click to show follow up.

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 an one-pass algorithm using only constant space?

代码:oj测试通过 Runtime: 47 ms
 1 class Solution:
 2     # @param A a list of integers
 3     # @return nothing, sort in place
 4     def sortColors(self, A):
 5         # None case
 6         if A is None:
 7             return None
 8         # two pointers: p0 for red and p2 for blue
 9         p0 = 0
10         p2 = len(A)-1
11         i = 0
12         while i <= p2 :
13             if A[i] == 0 :
14                 A[i],A[p0] = A[p0],A[i]
15                 i += 1
16                 p0 += 1
17             elif A[i] == 1 :
18                 i += 1
19             else :
20                 A[i],A[p2] = A[p2],A[i]
21                 p2 -= 1

思路

这道题的要求跟很多数组题的要求类似,如何遍历一遍数组就完成任务。

就是头尾各放两个指针。

因为有三种类型的元素,所以需要守住头尾两个指针就可以了。

很多数组题目都是利用交换元素值的技巧,遍历一次就可以了。

转载于:https://www.cnblogs.com/xbf9xbf/p/4246295.html

leetcode 【 Sort Colors 】python 实现相关推荐

  1. [leetcode]Sort Colors

    问题描写叙述: Given an array with n objects colored red, white or blue, sort them so that objects of the s ...

  2. LeetCode Sort Colors

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

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

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

  4. 【排序】LeetCode 75. Sort Colors

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

  5. Leetcode 075 Sort Colors

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

  6. [LeetCode]75.Sort Colors

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

  7. Sort Colors - LeetCode

    Sort Colors - LeetCode 题目: Given an array with n objects colored red, white or blue, sort them so th ...

  8. 75. Sort Colors - LeetCode

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

  9. LeetCode刷题Python实录

    使用Python的LeetCode刷题 前言 题目 1408. 数组中的字符串匹配 508. 出现次数最多的子树元素和 1089. 复写零 剑指 Offer 14- I. 剪绳子 1175. 质数排列 ...

  10. 82 颜色分类II-彩虹排序(Sort Colors II)

    文章目录 1 题目 2 解决方案 2.1 思路 2.2 图解 2.3 时间复杂度 2.4 空间复杂度 3 源码 1 题目 题目:颜色分类II(Sort Colors II) 描述:给定一个有n个对象( ...

最新文章

  1. 域渗透提权之MS14-068
  2. pandas计算特征与所有数值特征的相关性并排序可视化:包含pearson、spearman、kendall
  3. html如何用v-for遍历,v-for循环遍历
  4. element隐藏组件滚动条scrollbar使用
  5. 工程制图 (组合体的视图与尺寸注法)
  6. [你必须知道的.NET]第三十四回,object成员,不见了!
  7. CSAPP--信息的表示与处理
  8. c语言第五章137页题目及答案,(完整版)C语言程序设计教程第五章练习题题目
  9. 在手机上查看和测试vue脚手架搭建的项目
  10. 刘强东解读京东AI战略布局,沈南鹏高文杨强助阵谈AI机遇
  11. 关于Ibatis中的executeForObject方法使用时,需要考虑空的问题!
  12. TweenLite中文帮助手册
  13. Linux文件编辑命令vi详细说明
  14. WEBSHELL权限提升技巧(综合版)
  15. WIN10+VS2015部署PanddleOCR
  16. 淘淘商城---8.6
  17. 机器学习中的损失函数(Loss Function)介绍、说明
  18. 云存储系统服务器版安装,云存储系统服务器版安装
  19. 【Linux】SSH相关命令
  20. 【视频架构day1】美拍短视频系统架构:1亿流量的应对之道

热门文章

  1. python带cookie登录
  2. 可视化技巧:分类问题中的决策面画法 (直观理解plt.contour的用法)
  3. IK Multimedia MODO BASS for mac(低音效果虚拟乐器)
  4. ICPC North Central NA Contest 2017 E - Is-A? Has-A? Who Knowz-A?
  5. mysql8.0 linux 安装
  6. 通过JS制作一个简易数码时钟
  7. 《社交网站界面设计(原书第2版)》——3.6 指纹或Bio指纹
  8. linux已经不存在惊群现象
  9. StringUtils 的常用方法
  10. OpenCV学习(7) 分水岭算法(1)