题目

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

思路

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

就是头尾各放两个指针。

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

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

leetcode 【 Sort Colors 】python 实现的更多相关文章

  1. [leetcode]Sort Colors @ Python

    原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, wh ...

  2. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  3. [LeetCode] Sort Colors 颜色排序

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

  4. [Leetcode] Sort Colors (C++)

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

  5. 75.[LeetCode] Sort Colors

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

  6. [LeetCode] Sort Colors 对于元素取值有限的数组,只遍历一遍的排序方法

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

  7. [LeetCode] Sort Colors 只有3个类型的排序

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

  8. [leetcode]Sort List @ Python

    原题地址:http://oj.leetcode.com/problems/sort-list/ 题意:链表的排序.要求:时间复杂度O(nlogn),空间复杂度O(1). 解题思路:由于题目对时间复杂度 ...

  9. LeetCode Sort Colors (技巧)

    题意: 一个数组只可能含有3种数据,分别为1,2,3,请将数组排序(只能扫一遍). 思路: 如果扫两遍的话,用个桶记录一下,再赋值上去就行了. class Solution { public: voi ...

  10. 【LeetCode】Sort Colors 数组排序

    题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...

随机推荐

  1. Python开发环境Wing IDE如何使用调试功能

    在使用Wing IDE开始调试的时候,需要设置断点的行,读取GetItemCount函数的返回.这可以通过单击行并选择Break工具栏条目,或通过单击行左边的黑色边缘.断点应该以实心红圈的形式出现: ...

  2. C#基础知识图谱

  3. 《Python高效开发实战》实战演练——基本视图3

    在完成Django项目和应用的建立后,即可以开始编写网站应用代码,这里通过为注册页面显示一个欢迎标题,来演示Django的路由映射功能. 1)首先在djangosite/app/views.py中建立 ...

  4. 初学基础python记录

    1.对于python来说,最重要的就是缩进.相当于其他语言的{}中括号. 2.转义快捷等 alt+p和alt+n来复制上下一行.变量使用时得先赋值,且大小写敏感,遵循变量命名规则.Python还允许用 ...

  5. hdu-2256 Problem of Precision---矩阵快速幂+数学技巧

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2256 题目大意: 题目要求的是(sqrt(2)+sqrt(3))^2n %1024向下取整的值 解题 ...

  6. Android(java)学习笔记77:Android中assets文件夹资源的访问

    Android资源文件分类: Android资源文件大致可以分为两种: 第一种是res目录下存放的可编译的资源文件: 这种资源文件系统会在R.java里面自动生成该资源文件的ID,所以访问这种资源文件 ...

  7. quartz调度

    http://www.cnblogs.com/lzrabbit/archive/2012/04/14/2446942.html

  8. oc描述器排序

    int main(int argc, const char * argv[]) { @autoreleasepool { NSArray *array = @[CreateDict(@"王思 ...

  9. 深入浅出:了解JavaScript的六种继承

    了解继承前我们需要了解函数的构造,方便我们理解. 常见六种继承方式: 1.原型继承call和apply: 2.原型拷贝:循环父函数protype的key值=子函数prototype的key值: 3.原 ...

  10. 快速搭建lvs + keepalived + nginx

      环境:   VIP         192.168.2.224 LVS        192.168.2.217     centos7 nginx1    192.168.2.231     c ...