-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
22 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|