mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
go: add «75. Sort Colors»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
1e48c99bfb
commit
fbc9fdb7ad
1 changed files with 17 additions and 0 deletions
17
go/sort-colors.go
Normal file
17
go/sort-colors.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
package sort_colors
|
||||
|
||||
func sortColors(nums []int) {
|
||||
l, r := 0, len(nums)-1
|
||||
|
||||
for i, _ := range nums {
|
||||
for l <= i && i <= r && nums[i] != 1 {
|
||||
if nums[i] == 0 {
|
||||
nums[i], nums[l] = nums[l], nums[i]
|
||||
l++
|
||||
} else if nums[i] == 2 {
|
||||
nums[i], nums[r] = nums[r], nums[i]
|
||||
r--
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue