码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode75 Sort Colors

时间:2020-02-18 23:20:24      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:cti   with   解法   solution   指针   tps   快排   ++   input   

  1 """
  2 Given an array 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.
  3 Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
  4 Note: You are not suppose to use the library‘s sort function for this problem.
  5 Example:
  6 Input: [2,0,2,1,1,0]
  7 Output: [0,0,1,1,2,2]
  8 """
  9 """
 10 绝世好题:提供了四种解法
 11 传送门:https://blog.csdn.net/qq_17550379/article/details/80499816
 12 1.计数排序
 13 2.三路快排(回顾liuyubobobo的视频第三章6-9)
 14 3.适合此题的三路排序算法
 15 4.三指针
 16 """
 17 """
 18 解法一:计数排序
 19 """
 20 class Solution1:
 21     def sortColors(self, nums):
 22         """
 23         Do not return anything, modify nums in-place instead.
 24         """
 25         count = [0, 0, 0]
 26         for i in nums:
 27             assert(i >= 0 and i <= 2)
 28             #python assert断言是声明布尔值必须为真的判定,如果发生异常就说明表达式为假。
 29             #可以理解assert断言语句为raise-if-not,用来测试表示式,其返回值为假,就会触发异常。
 30             count[i] += 1
 31         index = 0
 32         for i, num in enumerate(count):
 33             for j in range(num):
 34                 nums[index] = i
 35                 index += 1
 36 
 37 """
 38 解法二:快速排序(三路),没有随机标兵点
 39 """
 40 class Solution2:
 41     def sortColors(self, nums: List[int]) -> None:
 42         """
 43         Do not return anything, modify nums in-place instead.
 44         """
 45         self._sortColors(nums, 0, len(nums)-1) #bug 没写-1
 46     def _sortColors(self, nums, l, r):
 47         if r <= l:
 48             return
 49         pivot = l #标定点
 50         lt = l #[l+1, lt] <val
 51         i = l+1 #[lt+1,gt-1] = val
 52         gt = r+1 #[gt, r] > val
 53         while i < gt:
 54             if nums[i] < nums[pivot]:
 55                 lt += 1
 56                 nums[i], nums[lt] = nums[lt], nums[i]
 57                 i += 1
 58             elif nums[i] > nums[pivot]:
 59                 gt -= 1
 60                 nums[i], nums[gt] = nums[gt], nums[i]  #注意与后面的交换不需要再加i
 61             else:
 62                 i += 1
 63         nums[pivot], nums[lt] = nums[lt], nums[pivot]
 64         self._sortColors(nums, l, lt-1) #快排交换放在了lt的位置
 65         self._sortColors(nums, gt, r)
 66 
 67 """
 68 解法三:适合此题的三路排序算法
 69 """
 70 class Solution3:
 71     def sortColors(self, nums):
 72         """
 73         Do not return anything, modify nums in-place instead.
 74         """
 75         zero = -1
 76         i = 0 #从0开始向后遍历
 77         two = len(nums)
 78         while i < two:
 79             if nums[i] == 1:
 80                 i += 1
 81             elif nums[i] == 2:
 82                 two -= 1
 83                 nums[two], nums[i] = nums[i], nums[two]
 84             else:
 85                 zero += 1
 86                 nums[zero], nums[i] = nums[i], nums[zero]
 87                 i += 1 #!!! forget
 88 """
 89 解法四:用三个指针
 90 i,j,k分别指向最后一个0,1,2
 91 """
 92 class Solution4:
 93     def sortColors(self, nums):
 94         """
 95         Do not return anything, modify nums in-place instead.
 96         """
 97         i, j, k = -1, -1, -1 #i,j,k分别指向最后一个0,1,2
 98         for m in range(len(nums)):
 99             if nums[m] == 0:
100                 k += 1         #bug python中写成 nums[++k] 是无效的
101                 nums[k] = 2
102                 j += 1
103                 nums[j] = 1
104                 i += 1
105                 nums[i] = 0
106             elif nums[m] == 1:
107                 k += 1
108                 nums[k] = 2
109                 j += 1
110                 nums[j] = 1
111             else:
112                 k += 1
113                 nums[k] = 2

 

leetcode75 Sort Colors

标签:cti   with   解法   solution   指针   tps   快排   ++   input   

原文地址:https://www.cnblogs.com/yawenw/p/12329292.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!