Skip to content

Commit 7fd680c

Browse files
authored
use range-for
range for is much easy to read
1 parent 000a261 commit 7fd680c

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

leetcode/0001.Two-Sum/1. Two Sum.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ package leetcode
22

33
func twoSum(nums []int, target int) []int {
44
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}
5+
for k, v := range nums {
6+
if idx, ok := m[target-v]; ok {
7+
return []int{idx, k}
98
}
10-
m[nums[i]] = i
9+
m[v] = k
1110
}
1211
return nil
1312
}

0 commit comments

Comments
 (0)