From fbc9fdb7adc2c699acac00994f6ad726366e21b5 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Wed, 12 Jun 2024 17:33:21 +0200 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB75.=20Sort=20Colors=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- go/sort-colors.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 go/sort-colors.go 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-- + } + } + } +}