【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 ...
随机推荐
- JavaScipt 事件体系
事件机制 jQuery对事件的绑定分别有几个API .bind()/.live()/.delegate()/.on() 不管是用什么方式绑定,归根到底还是用addEventListener/attac ...
- MVC中获取来自控制器名称与动作的方法
#region 获取控制器名称与动作 protected void GetNameSpace() { var nameSpace = this.RouteData.Values["contr ...
- MySql学习 (一) —— 基本数据库操作语句、三大列类型
注:该MySql系列博客仅为个人学习笔记. 在使用MySql的时候,基本都是用图形化工具,如navicat.最近发现连最基本的创建表的语法都快忘了... 所以,想要重新系统性的学习下MySql,为后面 ...
- μC/OS-Ⅲ系统的中断管理
一.典型的μC/OS-Ⅲ中断服务程序解析 μC/OS-Ⅲ系统中典型有内核参与中断服务程序示例如下: MyISR: ...
- 有关利用python获取网页, 以及KDD近几年论文标题与摘要链接
最近一直在看KDD的论文,不过,由于老师并没有什么合理的方向性,所以考虑把kdd中的大部分内容都利用python将所有标题.摘要获取下来. 还有一个原因在于,看acm上的摘要,都只显示了两行,再看多点 ...
- 使用VMware Workstation 12.5.2新建虚拟机
关于VMware版本:VMware10可以支持32位和64位操作系统,VMware11及以上版本只能支持64位Win7及以上版本的操作系统!同时,VMware Workstation 10.0正式版发 ...
- Maven仓库
http://search.maven.org/#search%7Cga%7C1%7C
- Android 设置ListView当前显示的item
项目中可能会有这种需求:动态设置ListView显示的item 这种需求可能会出现在不同的情况下,有的是打开页面就要显示在特定的位置,也有的是浏览列表时实时更新数据并且改变了集合中数据,或者是某种条件 ...
- 制造行业流程管理的“IPO”思维
流程管理是企业从流程角度出发,关注流程是否增值的一套管理体系.从认识流程.到建立流程.到管理流程.再到优化流程,企业管理人员要去除不增值和低价值的流程,减少员工犯错误的机会,建立一套卓越的流程体系. ...
- android退出MainActivity后onDestroy不回调
问题:有时点击back键,退出MainActivity后,app已不可见,但是onDestroy却没有被调用. 原因: 1. onBackPressed被覆盖 @Override public voi ...