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 ...
随机推荐
- Android商城开发系列(十一)—— 首页秒杀布局实现
首页秒杀布局如下图: 布局使用的是LinearLayout和RecyclerView去实现,新建seckkill_item.xml,代码如下所示: <?xml version="1.0 ...
- 关于wp8.1 runtime模式下面的摄像头调用拍照问题和应用生命周期问题
现在的msdn文档,还找不到详细的wp8.1的摄像头拍照文档,只有一个序列拍照,类似九连拍的文档,而且这文档感觉就是windows8.1搬过来应付的,wp8.1模式,只要有一个地方处理不好,手机就会死 ...
- IOS UITextFieldDelegate (常用的代理方法)
#pragma mark - UITextFieldDelegate // 返回NO代表着文本输入框不可以改变(不可以编辑) - (BOOL)textField:(UITextField *)text ...
- Android(java)学习笔记101:Java程序入口和Android的APK入口
1. Java程序的入口:static main()方法 public class welcome extends Activity { @Override public void onCreate( ...
- 关于mongodb的日志
mongodb的日志与profile相似,在启动mongod时 可以用verbose这个参数配置他的日志详细程度,分为一个v到5个v,其中v越多,详细度越高 mogod.conf port = d ...
- 移动端调试利器-vConsole
现在移动端开发越来越火,随之而来的问题也越来越多,今天给大家介绍一款移动端调试神器,vconsole. 一.先引用文件,可以从https://www.bootcdn.cn/vConsole/下载,也可 ...
- java算法面试题:递归算法题2 第1个人10,第2个比第1个人大2岁,依次递推,请用递归方式计算出第8个人多大?
package com.swift; public class Digui_Return { public static void main(String[] args) { /* * 递归算法题2 ...
- Spring Cloud 入门 Eureka-Server服务注册
这里就不介绍怎么创建springboot项目了,可以查看我前面的博客 Spring Cloud Eureka Spring Cloud Eureka是Spring Cloud Netflix项目下的服 ...
- restful api 规范
- GPIO实现I2C协议模拟(2)
接着上一节继续补充 结合上一节的描述 写Slave的过程如下(BYTE) 读Slave的过程如下(BYTE) 分为两段 第一段 ,写OFFSET,第二段读数据 WORD的方式与BYTE大同异 读行为 ...