We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 000a261 + 7fd680c commit 6cb397fCopy full SHA for 6cb397f
leetcode/0001.Two-Sum/1. Two Sum.go
@@ -2,12 +2,11 @@ package leetcode
2
3
func twoSum(nums []int, target int) []int {
4
m := make(map[int]int)
5
- for i := 0; i < len(nums); i++ {
6
- another := target - nums[i]
7
- if _, ok := m[another]; ok {
8
- return []int{m[another], i}
+ for k, v := range nums {
+ if idx, ok := m[target-v]; ok {
+ return []int{idx, k}
9
}
10
- m[nums[i]] = i
+ m[v] = k
11
12
return nil
13
0 commit comments