diff --git a/go/sort-colors.go b/go/sort-colors.go new file mode 100644 index 0000000..be5c431 --- /dev/null +++ b/go/sort-colors.go @@ -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-- + } + } + } +}