【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 ...
随机推荐
- 【NodeJS线程】Boss和他的职员们
>>>[说明]还是一如既往的,这篇文章是从我的个人博客里挪过来的.原文参见:http://www.jscon.co/coding/frontend/nodejs_fork_child ...
- 读javascript高级程序设计00-目录
javascript高级编程读书笔记系列,也是本砖头书.感觉js是一种很好上手的语言,不过本书细细读来发现了很多之前不了解的细节,受益良多.<br/>本笔记是为了方便日后查阅,仅作学习交流 ...
- 时间控件之赋值问题:datetimebox
1.datetimebox不显示毫秒数: <input class="easyui-datetimebox" name="birthday" data-o ...
- merge,join,concat
merge交集 join并集 concat axis=0 竖着连 axis=1 横着连
- WPF附加属性
附加属性实质也是依赖属性,是说一个属性本来不属于某个对象,但由于某种需求被后来附加上的,也就是说把对象放入一个特定环境后才具有的属性 例子:人在学校有年纪和班级两个属性,人放在学校里会获得年级和班级两 ...
- The hierarchy of the type is inconsistent错误问题
在springMVC的AOP 面向切面编程中,引用: package com.ah.aop; import java.lang.reflect.Method; import org.springfra ...
- <jsp:include page="" />路径
填写绝对路径:page="/WEB-INF/folder1/folder2/.../fileName.jsp"
- Java中使用二重循环打印沙漏图形
1.首先判断外层,A .B.C.D都符合条件 2.那么再看内层 A :int i=0;i<5;i++ 当i=1时;带入到第二个内层循环中 int j=0;j<Math.abs(i)*2+1 ...
- ssi项目(1)环境搭建
1.环境准备 导包(jdk1.8只支持spring4.0以上的版本) mysql驱动包 c3p0驱动包 mybatis包 spring-core.spring-aop.spring-web.sprin ...
- 【初级】linux cp 命令详解及使用方法实战
cp:复制文件或者目录 前言: cp命令用来复制文件或者目录,是Linux系统中最常用的命令之一.一般情况下,shell会设置一个别名,在命令行下复制文件时,如果目标文件已经存在,就会询问是否覆盖,不 ...