aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorterminaldweller <thabogre@gmail.com>2022-12-05 06:32:11 +0000
committerterminaldweller <thabogre@gmail.com>2022-12-05 06:32:11 +0000
commit590adbd47a254a2bbd3b646a0595cd7aa2d8b7c5 (patch)
tree68a1d843f69f09567ead8cc788aff62f65f9bf83
parent2256 (diff)
downloadleetcode-590adbd47a254a2bbd3b646a0595cd7aa2d8b7c5.tar.gz
leetcode-590adbd47a254a2bbd3b646a0595cd7aa2d8b7c5.zip
1
-rw-r--r--1/go.mod3
-rw-r--r--1/main.go36
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))
+}