diff options
-rw-r--r-- | 1/go.mod | 3 | ||||
-rw-r--r-- | 1/main.go | 36 |
2 files changed, 39 insertions, 0 deletions
diff --git a/1/go.mod b/1/go.mod new file mode 100644 index 0000000..2d5a4a6 --- /dev/null +++ b/1/go.mod @@ -0,0 +1,3 @@ +module 1 + +go 1.18 diff --git a/1/main.go b/1/main.go new file mode 100644 index 0000000..598041e --- /dev/null +++ b/1/main.go @@ -0,0 +1,36 @@ +package main + +import "fmt" + +func twoSum(nums []int, target int) []int { + dict := make(map[int][]int, len(nums)) + for i := 0; i < len(nums); i++ { + dict[nums[i]] = append(dict[nums[i]], i) + } + // fmt.Println(dict) + + if target%2 == 0 { + if _, ok := dict[target/2]; ok { + // fmt.Println(len(dict[target/2])) + if len(dict[target/2]) >= 2 { + // fmt.Println("one") + return []int{dict[target/2][0], dict[target/2][1]} + } + } + } + for key, val := range dict { + if _, ok := dict[target-key]; ok { + if val[0] != dict[target-key][0] { + return []int{val[0], dict[target-key][0]} + } + } + } + + return []int{100, 100} +} + +func main() { + fmt.Println(twoSum([]int{2, 7, 11, 15}, 9)) + fmt.Println(twoSum([]int{3, 2, 4}, 6)) + fmt.Println(twoSum([]int{3, 3}, 6)) +} |