Skip to content

Commit

Permalink
feat: two sum
Browse files Browse the repository at this point in the history
  • Loading branch information
kimi0230 committed Mar 2, 2024
1 parent 0d4fa89 commit 94c39bd
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Leetcode_labuladong/1.两数之和.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* @lc app=leetcode.cn id=1 lang=golang
*
* [1] 两数之和
*/

// @lc code=start
// 用一個map, key紀錄number的值, value 紀錄number的index
// 遍歷整個nums, 判斷map中是否有 "target-number"存入map, 如果有就可以將 index從map取出, 並回傳
func twoSum(nums []int, target int) []int {
m := make(map[int]int)
for i, v := range nums {
if j, ok := m[target-v]; ok {
return []int{j, i}
}
m[v] = i
}
return nil
}

// @lc code=end

0 comments on commit 94c39bd

Please sign in to comment.