Created
June 21, 2020 07:13
-
-
Save Irene-123/7b3c4853e78378c3f2e23c111f300931 to your computer and use it in GitHub Desktop.
Leetcode solution-Sort Colors .Dutch National Flag problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```python3 | |
#https://en.wikipedia.org/wiki/Dutch_national_flag_problem | |
#https://leetcode.com/problems/sort-colors/ | |
def sortColors(self, nums): | |
red, white, blue = 0, 0, len(nums)-1 | |
while white <= blue: | |
if nums[white] == 0: | |
nums[red], nums[white] = nums[white], nums[red] | |
white += 1 | |
red += 1 | |
elif nums[white] == 1: | |
white += 1 | |
else: | |
nums[white], nums[blue] = nums[blue], nums[white] | |
blue -= 1 | |
``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment