题目分析:

  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. DD打卡

    一.安装逍遥安卓模拟器 二.安装钉钉 三.设置当前GPS座标 位置模拟器: 链接: https://pan.baidu.com/s/1TC5QkrGAgHOJWtzJnX6vhA 提取码: bpu8 ...

  2. 整理Webview加载缓慢的解决方案

    1.https://www.cnblogs.com/xinye/p/3144139.html 2.https://www.jianshu.com/p/95d4d73be3d1

  3. 通过yum仓库安装mysql

    1,下载安装包wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm2,安装mysql源yum -y ins ...

  4. Tost元素识别

    在日常使用App过程中,经常会看到App界面有一些弹窗提示(如下图所示)这些提示元素出现后等待3秒左右就会自动消失,那么我们该如何获取这些元素文字内容呢? Toast简介 Android中的Toast ...

  5. vue: This relative module was not found

    这是今天运行vue项目报的一个错误,特地在此记录一下. 错误信息如下: ERROR Failed to compile with 1 errors This relative module was n ...

  6. PIPE、SIGNAL(day11)

    一.管道 管道分为两种: 无名管道 有名管道 无名管道用于具有亲缘关系的进程间通讯.无名管道是单工的. 有内核管理的一块内存空间. 使用管道,系统提供了pipe() #include <unis ...

  7. JDK环境变量设置(linux)

    1.下载jdk1.8版本软件包 2.解压 tar -zxvf jdk1.8.tar.gz mv jdk1.8 /usr/local/ 3.添加环境变量 vim /etc/profile 在文件底部加入 ...

  8. Python学习笔记之类与对象

    这篇文章介绍有关 Python 类中一些常被大家忽略的知识点,帮助大家更全面的掌握 Python 中类的使用技巧 1.与类和对象相关的内置方法 issubclass(class, classinfo) ...

  9. 转载 - KMP算法祥解

    出处:http://www.cppblog.com/oosky/archive/2006/07/06/9486.html KMP字符串模式匹配详解 来自CSDN     A_B_C_ABC 网友 KM ...

  10. 11gR2 集群(CRS/GRID)新功能—— SCAN(Single Client Access Name)

    https://blogs.oracle.com/database4cn/11gr2-crsgrid-scansingle-client-access-name