leetcode 【 Sort Colors 】python 实现
题目:
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.
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?
class Solution:
# @param A a list of integers
# @return nothing, sort in place
def sortColors(self, A):
# None case
if A is None:
return None
# two pointers: p0 for red and p2 for blue
p0 = 0
p2 = len(A)-1
i = 0
while i <= p2 :
if A[i] == 0 :
A[i],A[p0] = A[p0],A[i]
i += 1
p0 += 1
elif A[i] == 1 :
i += 1
else :
A[i],A[p2] = A[p2],A[i]
p2 -= 1
思路:
这道题的要求跟很多数组题的要求类似,如何遍历一遍数组就完成任务。
就是头尾各放两个指针。
因为有三种类型的元素,所以需要守住头尾两个指针就可以了。
很多数组题目都是利用交换元素值的技巧,遍历一次就可以了。
leetcode 【 Sort Colors 】python 实现的更多相关文章
- [leetcode]Sort Colors @ Python
原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, wh ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- [LeetCode] Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [Leetcode] Sort Colors (C++)
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 75.[LeetCode] Sort Colors
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- [LeetCode] Sort Colors 对于元素取值有限的数组,只遍历一遍的排序方法
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [LeetCode] Sort Colors 只有3个类型的排序
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [leetcode]Sort List @ Python
原题地址:http://oj.leetcode.com/problems/sort-list/ 题意:链表的排序.要求:时间复杂度O(nlogn),空间复杂度O(1). 解题思路:由于题目对时间复杂度 ...
- LeetCode Sort Colors (技巧)
题意: 一个数组只可能含有3种数据,分别为1,2,3,请将数组排序(只能扫一遍). 思路: 如果扫两遍的话,用个桶记录一下,再赋值上去就行了. class Solution { public: voi ...
- 【LeetCode】Sort Colors 数组排序
题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...
随机推荐
- 《Python高效开发实战》实战演练——建立应用2
为了在项目中开发符合MVC架构的实际应用程序,需要在项目中建立Django应用.每个Django项目可以包含多个Django应用.建立应用的语法为: #python manage.pystartapp ...
- uvm_reg_adapter——寄存器模型(十八)
uvm_reg_adapter 功能就是在uvm_reg_bus_op和总线操作之间的转换.主要包含两个函数reg2bus 和bus2reg. //-------------------------- ...
- zip man man.config
zip man man.config zip -r zip1 man.config man.zip gzip a tar -cvf test.tar /home/* tar -tf test.tar ...
- python之__init__使用方法
定义类的时候,若是添加__init__方法,那么在创建类的实例的时候,实例会自动调用这个方法,一般用来对实例的属性进行初使化.比如:class testClass: def __init__(self ...
- COGS 1453. [USACO NOV]空牛栏
★★ 输入文件:empty.in 输出文件:empty.out 简单对比时间限制:1 s 内存限制:64 MB [题目描述] FJ建的新牛棚里有N(2<=N<=3,000, ...
- ABAP和Java的destination和JNDI
Netweaver里使用事务码SM59创建Destination: Java 新建一个destination: 测试代码: try { Context ctx = new InitialContext ...
- 【转】Nginx搭建反向代理服务器过程详解
阅读目录 1.1 反向代理初印象 1.2 反向代理的作用 2.1 Nginx是神马? 2.2 Nginx的应用现状 2.3 Nginx的核心特点 3.1 准备一个ASP.NET网站部署到IIS服务器集 ...
- PMP(第六版)中的沟通方法总结与对比
- 常量池与方法区以及又读new String对象创建问题
又拿出这道String str1 = new String("abc");创建几个对象的面试题梳理了一下常量池与方法区的关系,希望能把这两者的关系通过这道面试题说明白 方法区是什么 ...
- ERR_FAILED 浏览器访问
我是针对上一篇的问题 继续的探究 ,我百度了 看了这 https://zhidao.baidu.com/question/1175643597811783659.html 之后我就用 电脑管家进行系 ...