LeetCode 75. Sort Colors (python一次遍历,模拟三路快排)
题目分析:
- 本题需要实现数字只包含0,1,2的排序,并且要求一次遍历。
- 由于只用把数字隔离开,很容易想到快排的分割操作partition。
- 将三类数字隔离开,也就是模拟三路快排的分割操作了。
解题思路
- 快排是选取哨兵p,将一段数组分成<p,=p,>p三类,并按这个顺序隔离开。
- 本题类似,哨兵为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一次遍历,模拟三路快排)的更多相关文章
- 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 计数排序,三路快排
解法一:计数排序:统计0,1,2 的个数 时间复杂度:O(n) 空间复杂度:O(k) k为元素的取值范围, 此题为O(1) class Solution { public: void sortC ...
- [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 ...
- [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(颜色排序)
翻译 给定一个包括红色.白色.蓝色这三个颜色对象的数组.对它们进行排序以使同样的颜色变成相邻的,其顺序是红色.白色.蓝色. 在这里,我们将使用数字0.1和2分别来代表红色.白色和蓝色. 原文 Give ...
- 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 in-place so that objects of the ...
随机推荐
- spring cloud(五) hystrix
开启feign 熔断 hystrix 整合hystrix-dashboard监控面板 1. 服务调用者boot工程 pom引入依赖 <!-- hystrix-dashboard 监控依赖 ...
- PHP实现文字写入图片功能
/** * PHP实现文字写入图片 */class wordsOnImg { public $config = null; /** * @param $config 传入参数 * @param $co ...
- 数据库Day3之SQL Server 触发器
最近在做一个人事管理系统写了几个简单的触发器 1.在删除员工信息表中员工信息时结果区提示被删除员工信息 create trigger teston 员工信息表after deleteasselect ...
- BZOJ 5466: [Noip2018]保卫王国 动态DP
Code: // luogu-judger-enable-o2 #include<bits/stdc++.h> #define ll long long #define lson (now ...
- jsp 判断当前时间是否符合设置的时间条件
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- C#那20道题
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- swift-自动计算字符串的宽高
写一个方法来继承String //自动控制文字换行及宽度 extension String { func textSizeWithFont(font: UIFont, constrainedToSiz ...
- css画圆
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...
- 如何设置div自适应高度
1.给div添加overflow属性 .div{ width:760px; overflow:hidden; } 2.其他的设置height:auto 等我测试没有效果
- Codeforces 947E Perpetual Subtraction (线性代数、矩阵对角化、DP)
手动博客搬家: 本文发表于20181212 09:37:21, 原地址https://blog.csdn.net/suncongbo/article/details/84962727 呜啊怎么又是数学 ...