leetcode75. Sort Colors

Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.

We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.

You must solve this problem without using the library's sort function.

Example 1:

Input: nums = [2,0,2,1,1,0]

Output: [0,0,1,1,2,2]

Example 2:

Input: nums = [2,0,1]

Output: [0,1,2]

快速排序的子过程 partition(重点在设计循环不变量)

方法一:排序

数组里只包含 0、1、2,因此可以对数组排序,排序以后,所有的 0 就被摆放在了一起,所有的 1 就被摆放在了一起,所有的 2 就被摆放在了一起。

复制代码
如果排序方法使用的是快速排序、归并排序,时间复杂度为 O(NlogN),这种方法题目不允许(代码省略);
又由于数组里只包含 0、1、2,还可以使用计数排序,时间复杂度为 O(N),这种方法题目不允许(代码省略)。

方法二:partition

题目最后给出的「进阶」要求,其实考察的是「快速排序」的子过程 partition,即:通过一次遍历,把数组分成三个部分。

写代码的时候需要注意到设置的变量以及区间的定义,也就是 循环不变量。循环不变量 简单说就是在循环的过程中保持不变的性质,这个性质是人为根据需要解决的任务定义的。

对 循环不变量 的简单认识:

复制代码
变量的值是变化的,但是保持不变的性质,就是循环不变量;
这里的「量」是一些人为定义的、可以判断真假的语句,在循环开始前、循环的过程中、循环结束以后,都为真;
这里的「循环」是广义上的,并不一定指「循环」,也有可能是在「递归」的过程中。
python 复制代码
from typing import List
class Solution:
    def sortColors(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """

        # all in [0, zero) = 0
        # all in [zero, i) = 1
        # all in [two, len - 1] = 2

        def swap(nums, index1, index2):
            nums[index1], nums[index2] = nums[index2], nums[index1]

        size = len(nums)
        if size < 2:
            return

        zero = 0
        two = size

        i = 0

        while i < two:
            if nums[i] == 0:
                swap(nums, i, zero)
                i += 1
                zero += 1
            elif nums[i] == 1:
                i += 1
            else:
                two -= 1
                swap(nums, i, two)
python 复制代码
from typing import List
class Solution:
    def sortColors(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """

        # all in [0, zero] = 0
        # all in (zero, i) = 1
        # all in (two, len - 1] = 2

        def swap(nums, index1, index2):
            nums[index1], nums[index2] = nums[index2], nums[index1]

        size = len(nums)
        if size < 2:
            return

        zero = -1
        two = size - 1

        i = 0

        while i <= two:
            if nums[i] == 0:
                zero += 1
                swap(nums, i, zero)
                i += 1
            elif nums[i] == 1:
                i += 1
            else:
                swap(nums, i, two)
                two -= 1
相关推荐
不想看见4048 分钟前
Search a 2D Matrix II数组--力扣101算法题解笔记
数据结构·算法
IronMurphy10 分钟前
【算法二十六】108. 将有序数组转换为二叉搜索树 98. 验证二叉搜索树
数据结构·算法·leetcode
InfiniSynapse1 小时前
连上Snowflake就能取数:InfiniSynapse + Spider2-Snow实战企业数据分析
数据结构·图像处理·人工智能·算法·语言模型·数据挖掘·数据分析
WolfGang0073211 小时前
代码随想录算法训练营 Day13 | 二叉树 part03
数据结构·算法·leetcode
91刘仁德2 小时前
C++ 内存管理
android·c语言·数据结构·c++·经验分享·笔记·算法
历程里程碑2 小时前
链表-----
数据结构·线性代数·算法·链表·矩阵·lua·perl
一叶落4382 小时前
167. 两数之和 II - 输入有序数组【C语言题解】
c语言·数据结构·算法·leetcode
Barkamin3 小时前
归并排序的简单实现
数据结构
我星期八休息3 小时前
深入理解哈希表
开发语言·数据结构·c++·算法·哈希算法·散列表
一叶落4383 小时前
LeetCode 54. 螺旋矩阵(C语言详解)——模拟 + 四边界收缩
java·c语言·数据结构·算法·leetcode·矩阵