作者: 负雪明烛
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)的更多相关文章

  1. 【LeetCode】Sort Colors 解题报告

    [题目] Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  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 ...

  3. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  4. C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)

    leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/  Total Accepted: 68702 Total ...

  5. leetCode 75.Sort Colors (颜色排序) 解题思路和方法

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  6. LeetCode 75. Sort Colors (python一次遍历,模拟三路快排)

    LeetCode 75. Sort Colors (python一次遍历,模拟三路快排) 题目分析: 本题需要实现数字只包含0,1,2的排序,并且要求一次遍历. 由于只用把数字隔离开,很容易想到快排的 ...

  7. [LeetCode] 75. Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  8. LeetCode 75. Sort Colors(排序颜色)

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  9. Leetcode 75. Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

随机推荐

  1. python—模拟生成双色球号

    双色球规则:"双色球"每注投注号码由6个红色球号码和1个蓝色球号码组成.红色球号码从1--33中不重复选择:蓝色球号码从1--16中选择. # -*- coding:UTF-8 - ...

  2. java面试题目偏基础

    一.JAVA基础篇-概念1.简述你所知道的Linux:Linux起源于1991年,1995年流行起来的免费操作系统,目前, Linux是主流的服务器操作系统, 广泛应用于互联网.云计算.智能手机(An ...

  3. C语言中的重要位运算

    1. 常用的等式 :-n = ~(n-1) = ~n + 1. 2. 获取整数n的人进制形式中的最后1个,也就是只保留最后一个1,其余的全部置位0,如1000 0011 --->  0000 0 ...

  4. TLSv1.3 Support:主流 Web 客户端和服务端对 TLSv1.3 的支持情况

    TLSv1.3 Support:主流 Web 客户端和服务端对 TLSv1.3 的支持情况 请访问原文链接:https://sysin.org/blog/tlsv1-3-support/,查看最新版. ...

  5. PLSQL导出oracle表结构和数据

    1.导出表结构和数据 方式1.tools->export user objects是导出表结构 tools ->export user object 选择选项,导出.sql文件 说明:导出 ...

  6. tableView和tableViewCell的背景颜色问题

    当在tableView中添加cell数据时,我们会发现原本设置的tableView的背景颜色不见了,这是因为加载cell数据时,tableView的背景颜色被cell数据遮盖住了,此时,可以通过设置c ...

  7. 用oracle中的Row_Number实现分页

    Row_Number实现分页   1:首先是 select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from table1 生成带序号 ...

  8. shell脚本 安全删除MySQL大表

    一.简介 源码地址 日期:2018/4/12 介绍:工具用于安全删除MySQL表,对于一些特定场景可能有用 应用场景:大批删除不走索引但是有主键的场景[可以是单列索引,也可是多列索引] 实现思路:根据 ...

  9. Dom 解析XML

    xml文件 <?xml version="1.0" encoding="UTF-8"?><data>    <book id=&q ...

  10. HCNP Routing&Switching之组播技术-组播协议IGMP

    前文我们了解了组播地址相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/15616740.html:今天我们来聊一聊组播协议中IGMP协议相关话题: 组播 ...