【LeetCode】75. Sort Colors 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/sort-colors/description/
题目描述
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.
Example:
Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
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?
题目大意
对乱序的0,1,2三个数字进行排序,保证结果是相同元素聚在一起。
解题方法
计数排序
因为只有三个数,所以简单的方法是计数排序。第一次遍历,统计出这三个数字出现的次数,第二次遍历,根据三个数字的次数对原列表进行修改。
from collections import Counter
class Solution(object):
def sortColors(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
count = Counter(nums)
for i in xrange(len(nums)):
if i < count[0]:
nums[i] = 0
elif i < count[0] + count[1]:
nums[i] = 1
else:
nums[i] = 2
双指针
我看到这个题的时候,很明显的看出是要把一个数组分成三段,分别是小于v,等于v和大于v。由于只有三个数字,所以也就是0,1,2分别聚在一起。所以,这个题的考点来自快排之三项切分快速排序。在《算法》第四版中有介绍,也可以看快速排序及三向切分快速排序。
下面是题目讲解:
如果只能扫一遍,很容易想到的就是左边存放0和1,右边存放2.两边往中间靠。
设置两个指针,zero和two;zero指向第一个1的位置(0串的末尾),two指向第一个非2的位置。然后用i对nums进行遍历:
然后使用i从头到尾扫一遍,直到与two相遇。
i遇到0就换到左边去,遇到2就换到右边去,遇到1就跳过。
需要注意的是:由于zero记录第一个1的位置,因此A[zero]与A[i]交换后,A[zero]为0,A[i]为1,因此i++,zero++;
而two记录第一个非2的位置,可能为0或1,因此A[two]与A[i]交换后,A[two]为2,A[i]为0或1,i不能前进,要后续判断。
class Solution(object):
def sortColors(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
zero = 0
two = len(nums) - 1
i = 0
while i <= two:
if nums[i] == 0:
nums[zero], nums[i] = nums[i], nums[zero]
i += 1
zero += 1
elif nums[i] == 1:
i += 1
elif nums[i] == 2:
nums[two], nums[i] = nums[i], nums[two]
two -= 1
日期
2018 年 2 月 27 日
2019 年 1 月 5 日 —— 美好的周末又开始了
【LeetCode】75. Sort Colors 解题报告(Python)的更多相关文章
- 【LeetCode】Sort Colors 解题报告
[题目] Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- LeetCode 75. 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 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)
leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/ Total Accepted: 68702 Total ...
- leetCode 75.Sort Colors (颜色排序) 解题思路和方法
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- LeetCode 75. Sort Colors (python一次遍历,模拟三路快排)
LeetCode 75. Sort Colors (python一次遍历,模拟三路快排) 题目分析: 本题需要实现数字只包含0,1,2的排序,并且要求一次遍历. 由于只用把数字隔离开,很容易想到快排的 ...
- [LeetCode] 75. Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- LeetCode 75. Sort Colors(排序颜色)
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- Leetcode 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
随机推荐
- linux 线程函数小结
由于主线程已经开始跑了,次线程还在使用串口打印需要一点时间,因此打印的都是重复的. #include "pthread.h" #include "stdio.h" ...
- lxml解析库的安装和使用
一.lxml的安装lxml是Python的一个解析库,支持HTML和XML的解析,支持XPath解析方式,而且解析效率非常高.本节中,我们了解一下lxml的安装方式,这主要从Windows.Linux ...
- CAN总线常见的两种编码格式(Intel/Motorola)
在汽车电子行业的开发或者测试中,我们经常会看到CAN总线信号的常见的两种编码格式:Intel格式与Motorola格式. 讲解这两种格式之前,我们先来了解一些大端模式和小端模式,会对后面理解这两种编码 ...
- android studio 生成aar和引用aar
以android studio 2.0正式版为例 1.aar包是Android studio下打包android工程中src.res.lib后生成的aar文件,aar包导入其他android stud ...
- [学习总结]9、Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
这个图片异步加载并缓存的类已经被很多开发者所使用,是最常用的几个开源库之一,主流的应用,随便反编译几个火的项目,都可以见到它的身影. 可是有的人并不知道如何去使用这库如何进行配置,网上查到的信息对于刚 ...
- @Order注解使用
注解@Order或者接口Ordered的作用是定义Spring IOC容器中Bean的执行顺序的优先级,而不是定义Bean的加载顺序,Bean的加载顺序不受@Order或Ordered接口的影响: @ ...
- Maven配置大全
maven项目打jar包(带依赖) <build> <plugins> <plugin> <artifactId>maven-assembly-plug ...
- 『学了就忘』Linux启动引导与修复 — 71、grub启动引导程序的加密
目录 1.什么是grub加密 2.grub加密步骤 3.grub加密的lock属性 1.什么是grub加密 上篇文章说了,系统在开机的时候,有一个5秒的读秒时间,方便你进入到grub界面中. 如下图所 ...
- linux 让.net 控制台后台运行
命令 nohup 你的shell命令 & 例如 nohup dotnet MQTTClient.dll & 输入完成后,终端会有提示 这时再按下回车 回到shell命 ...
- Apache Hudi 与 Hive 集成手册
1. Hudi表对应的Hive外部表介绍 Hudi源表对应一份HDFS数据,可以通过Spark,Flink 组件或者Hudi客户端将Hudi表的数据映射为Hive外部表,基于该外部表, Hive可以方 ...