【LeetCode】Sort Colors
Sort Colors
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?
Solution:
扫描两趟算法:
即 counting sort,第一趟给 0、1、2 分别计数,第二趟直接在相应位置上 overwrite value。
扫描一趟算法:
由于整个array只有三个值,我们实际上需要做的工作便是把所有的 0 移到 array 头部位置,把所有的 2 移到 array 尾部位置,剩下的 1 便自然被挪到了中间位置。
考虑两个指针,一个head指示“0头部”即将要“占领”到的位置,初始值为0,递增;另一个tail指示“2尾部”即将要“占领”到的位置,初始值为 len(nums)-1,递减。
从左至右扫描,当扫描到当前位置 i 时,如果值为 0 并且不在“0头部”( i >= head )时,此时需要把 i 与 head 进行值交换,“0头部”长度自然拓展1,而 i 位置上的值变得不确定,此时保持 i 的值不变;如果值为 1 并且不在“2尾部”( i <= tail )时,此时需要把 i 与 tail 进行值交换,“2尾部”长度自然拓展1,而 i 位置上的值变得不确定,此时保持 i 的值不变;其他情况(在“0头部”或“2尾部”,或者当前位置值为1)则直接把扫描指针 i 加 1 即可。
最后,本来扫描可以一直进行到 len(nums)-1,而由于扫描时已经可以确定“2尾部”中的2全部都已经在合适的位置上,所以扫描到 tail 即可终止循环( i <= tail )。
代码如下:
class Solution(object):
def sortColors(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
i, head, tail =0, 0, len(nums)-1
while i <= tail:
if nums[i] == 0 and i >= head:
nums[i], nums[head] = nums[head], nums[i]
head += 1
elif nums[i] == 2 and i <= tail:
nums[i], nums[tail] = nums[tail], nums[i]
tail -= 1
else:
i += 1
【LeetCode】Sort Colors的更多相关文章
- 【LeetCode】Sort Colors 解题报告
[题目] Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【LeetCode】Sort Colors 数组排序
题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...
- 【leetcode】Sort Colors(middle)☆
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 【Leetcode】Sort List JAVA实现
Sort a linked list in O(n log n) time using constant space complexity. 1.分析 该题主要考查了链接上的合并排序算法. 2.正确代 ...
- 【LeetCode】 sort list 单清单归并
称号:Sort a linked list in O(n log n) time using constant space complexity. 思路:要求时间复杂度O(nlogn) 知识点:归并排 ...
- 【数组】Sort Colors
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【leetcode】Sort List (middle)
Sort a linked list in O(n log n) time using constant space complexity. 思路: 用归并排序.设输入链表为S,则先将其拆分为前半部分 ...
- 【leetcode】Sort List
Sort List Sort a linked list in O(n log n) time using constant space complexity. 需要采用归并排序对链表进行操作. ...
- 【Leetcode】Sort List (Sorting)
这个问题需要与归并排序排两个名单,基本思路分为切割与合并 合并后的代码Merge Two Sorted List里已经讲得非常清楚了. 所以这里直接给出代码. public ListNode merg ...
随机推荐
- 纯JS 将table表格导出到excel
html <div > <button type="button" onclick="getXlsFromTbl('tableExcel','myDiv ...
- jquery 格式化日期
function setMaxEndDate(){ var beginDate=$("#beginDate").val(); var time = new Date(beginDa ...
- [原创]在Docker上部署mongodb分片副本集群。
一.安装docker. 请参考:http://www.cnblogs.com/hehexiaoxia/p/6150584.html 二.编写dockerfile. 1.在根目录下创建mongod的do ...
- The type String cannot be constructed. You must configure the container to supply this value.
利用 Enterprise Library 5.0 Microsoft.Practices.EnterpriseLibrary.Common Microsoft.Practices.Enterpris ...
- scala 打印一个乘法口诀表 (<<scala 编程>> P87)
(for(i <- 1 to 9;j <- 1 to i; s = s"$j*$i=${i*j}\t") yield {if(j==1) s"$s\n&quo ...
- Selenium的延迟等待
http://my.oschina.net/u/928852/blog/98885 Selenium的延迟等待分为 显式等待(Explicit Wait) & 隐式等待(Implicit Wa ...
- <java基础学习>02JAVA的基础组成(2)
60000-0000 0000-0000 0000-0000 0000-0110 0000-0110 -6这个数的正数的二进制取反,再加1 0000-0110取反: 1111-1001 + 0000- ...
- Codeforces Round #313 (Div. 1)
官方英文题解:http://codeforces.com/blog/entry/19237 Problem A: 题目大意: 给出内角和均为120°的六边形的六条边长(均为正整数),求最多能划分成多少 ...
- mysql TIMESTAMP 设置为可NULL字段
今天遇到问题是mysql新建表的时候TIMESTAMP 类型的字段 默认是NOT NULL 然后上网查了一下 发现 很多都说 就是不能为NULL的 这都什么心态 其实设置为空很简单 只要在字段后面加上 ...
- mongodb 备份 还原 导出 导入
张映 发表于 2013-12-03 分类目录: nosql 标签:mongodb, 备份, 导入, 导出, 还原 mongodb数据备份和还原主要分为二种,一种是针对于库的mongodump和mong ...