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 ...
随机推荐
- Visual Studio 2017 如何打开Model Browser(实体数据模型浏览器)
写一个笔记,记录下在Visual Studio 2017中打开EF模型浏览器的步骤和方法,方便以后忘记了可以重新查阅.主要是现在VS功能越来越多,很多功能模块/界面要开启都是有先决条件,总之隐藏的很深 ...
- [转]C#中StreamReader读取中文出现乱码
摘自:C#中StreamReader读取中文出现乱码 原因是自Windows 2000之后的操作系统在文件处理时默认编码采用Unicode所以.NET文件的默认编码也是Unicode.除非另外指定,S ...
- ASP.NET中 前后台方法的相互调用
后台调用前台js方法: this.Page.ClientScript.RegisterStartupScript(this.GetType(), "js", "ShowM ...
- linux 命令——51 lsof(转)
lsof(list open files) 是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件.所以 如传输控制 ...
- raspberrypi&linux
Raspberrypi&linux 2018-01-23 19:54:01 Let's go!
- xtrabackup支持的engine
xtrabackup支持的engine 1.InnoDB/XtraDB Hot backup 2.MyISAM with read-lock 3.Archive,CSV with read-l ...
- Android(java)学习笔记74:ListViewProject案例(ListView + ArrayAdapter)
1. 首先是MainActivity.java文件,如下: package com.himi.lv1; import java.util.ArrayList; import java.util.Lis ...
- js 封装父页面子页面交互接口
定义标准接口 Interface= {}; Interface.ParentWin = {}; Interface.ChildWin = {}; /** * 父页面提供的标准接口函数名称 */ Int ...
- MFC-[转]基于MFC的ActiveX控件开发
作者:lidan | 出处:博客园 | 2012/3/13 16:10:34 | 阅读22次 ActiveX 控件是基于组件对象模型 (COM) 的可重用软件组件,广泛应用于桌面及Web应用中.在VC ...
- Optional int parameter 'fundID' is present but cannot be translated into a null value due to being declared as a primitive type
错误的意思是: "可选的int参数'fundID'存在但由于被声明为基本类型而无法转换为空值" 意思是fundID被申明为int的基本数据类型, 不能转换为字符串的null值. 难 ...