题目分析:

  1. 本题需要实现数字只包含0,1,2的排序,并且要求一次遍历。
  2. 由于只用把数字隔离开,很容易想到快排的分割操作partition。
  3. 将三类数字隔离开,也就是模拟三路快排的分割操作了。

解题思路

  1. 快排是选取哨兵p,将一段数组分成<p,=p,>p三类,并按这个顺序隔离开。
  2. 本题类似,哨兵为1,将一段数组分成0,1,2,即<1,=1,>1,并按这个顺序隔离开。

代码如下

class Solution:
def sortColors(self, nums: List[int]) -> None:
left = 0
#mid表示目前第一个1的位置,在len(nums)表示1还未出现
#加入1的操作,只用将mid-1,然后与left交换
mid = len(nums)
#right表示目前第一个2的位置,在len(nums)表示第一个2还未出现
#加入2的操作,只用将right-1,然后与left交换
right = len(nums) #left是一个游标,不断交换,直到nums[left]=0
while left < mid:
if nums[left] == 0:
left += 1
elif nums[left] == 1:
mid -= 1
temp = nums[left]
nums[left] = nums[mid]
nums[mid] = temp
elif nums[left] == 2:
right -= 1
temp = nums[left]
nums[left] = nums[right]
nums[right] = temp
# 处理出现了2,但是还未出现1的情况
if mid > right:
mid = right

LeetCode 75. Sort Colors (python一次遍历,模拟三路快排)的更多相关文章

  1. LeetCode 75. Sort Colors (颜色分类):三路快排

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

  2. leetcode 75 Sort Colors 计数排序,三路快排

    解法一:计数排序:统计0,1,2 的个数 时间复杂度:O(n) 空间复杂度:O(k)    k为元素的取值范围, 此题为O(1) class Solution { public: void sortC ...

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

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

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

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

  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三色排序

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

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

    翻译 给定一个包括红色.白色.蓝色这三个颜色对象的数组.对它们进行排序以使同样的颜色变成相邻的,其顺序是红色.白色.蓝色. 在这里,我们将使用数字0.1和2分别来代表红色.白色和蓝色. 原文 Give ...

  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 in-place so that objects of the ...

随机推荐

  1. PAT_A1034#Head of a Gang

    Source: PAT A1034 Head of a Gang (30 分) Description: One way that the police finds the head of a gan ...

  2. Codeforces Round #548 (Div. 2) C. Edgy Trees

    You are given a tree (a connected undirected graph without cycles) of 

  3. (C/C++学习)1.C++中vector的使用

    说明:vector是C++中一个非常方便的容器类,它用于存放类型相同的元素,利用成员函数及相关函数可以方便的对元素进行增加或删除,排序或逆序等等,下面将对这些功能一一叙述. 一.vector的第一种用 ...

  4. 35.multi-index和multi-type搜索模式

        一.multi-index和multi-type搜索模式 /_search:所有索引,所有type下的所有数据都搜索出来 /index1/_search:指定一个index,搜索其下所有typ ...

  5. Pycharm中Git、Github的简单使用和配置

    Pycharm中Git.Github的使用 PyCharm本身自带了git,稍微配置一下就可以很好的在图形界面下进行Python项目的版本控制 配置Git 在配置前先新建一个项目,当然也可以打开已有的 ...

  6. python实现RGB转化为灰度图像

    问题: 我正尝试使用matplotlib读取RGB图像并将其转换为灰度.在matlab中,我使用这个: 1 img = rgb2gray(imread('image.png')); 在matplotl ...

  7. 【ACM】hdu_zs2_1005_Problem E _201308030747

    Problem E Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)Total Subm ...

  8. Find the Clones Trie Tree

    Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 8306   Accepted: 3130 Description Doubl ...

  9. cogs 9. 中心台站建设。。。

    9. 中心台站建设 ★★☆   输入文件:zpj.in   输出文件:zpj.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述]     n个城市之间有通讯网络,从这n个城 ...

  10. mybatis mapper xml文件配置resultmap时,id行和result行有什么区别?

    mybatis mapper xml文件配置resultmap时,id行和result行有什么区别? <resultMap id = "CashInvoiceMap" typ ...