C C++ Py3使用荷蘭旗演算3 pointer解Leetcode 75 Sort Colors
一次走訪演算採用於荷蘭旗演算法。
------------
one-pass algorithm is applied the algorithm for Dutch national flag.
[codes on Leetcode]https://leetcode.com/problems/sort-colors/solutions/6751459/counting-sort-vs-dutsch-flag-with-explain-c-c-py3-beats-100/
[Python C++採陣列排序速解字串問題Leetcode 451 Sort Characters By Frequency]https://www.youtube.com/watch?v=YfS4OMooZes
- class Solution:
- def sortColors(self, nums: List[int]) -> None:
- """
- Do not return anything, modify nums in-place instead.
- """
- red, white, blue=0, 1, 2
- l, m, r=0, 0, len(nums)-1
- while m<=r:
- x=nums[m]
- if x==red:
- nums[l], nums[m]=nums[m], nums[l]
- l+=1
- m+=1
- elif x==white:
- m+=1
- else:
- nums[m], nums[r]=nums[r], nums[m]
- r-=1
沒有留言:
張貼留言