From c7b0da5b641787f9ef2a373a7d773628c8128537 Mon Sep 17 00:00:00 2001 From: ductnn Date: Tue, 26 Mar 2024 10:13:35 +0700 Subject: [PATCH] add sol --- leetcode/41.FirstMissingPositive/sol.go | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 leetcode/41.FirstMissingPositive/sol.go diff --git a/leetcode/41.FirstMissingPositive/sol.go b/leetcode/41.FirstMissingPositive/sol.go new file mode 100644 index 0000000..647f6aa --- /dev/null +++ b/leetcode/41.FirstMissingPositive/sol.go @@ -0,0 +1,27 @@ +// https://leetcode.com/problems/first-missing-positive + +package main + +import "fmt" + +func firstMissingPositive(nums []int) int { + m := make(map[int]bool) + + for _, num := range nums { + if num > 0 { + m[num] = true + } + } + + for i := 1; ; i++ { + if !m[i] { + return i + } + } +} + +func main() { + nums := []int{3, 4, -1, 1} + + fmt.Println(firstMissingPositive(nums)) +}