Leetcode 75. Sort Colors

Posted 周洋的Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 75. Sort Colors相关的知识,希望对你有一定的参考价值。

class Solution:
    def sortColors(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        l, r = 0, len(nums) - 1
        j = 0
        while j < len(nums):
            if nums[j] == 0 and j >= l:
                nums[l], nums[j] = nums[j], nums[l]
                l += 1
            elif nums[j] == 2 and j <= r:
                nums[r], nums[j] = nums[j], nums[r]
                r -= 1
            else:
                j += 1
            if not l<r:
                break

 

以上是关于Leetcode 75. Sort Colors的主要内容,如果未能解决你的问题,请参考以下文章

[leetcode-75-Sort Colors]

Leetcode 75. Sort Colors

LeetCode 75 Sort Colors

LeetCode 75 Sort Colors

leetcode75 Sort Colors

LeetCode75 Sort Colors