mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 08:27:30 +08:00
use range-for
range for is much easy to read
This commit is contained in:
@ -2,12 +2,11 @@ package leetcode
|
||||
|
||||
func twoSum(nums []int, target int) []int {
|
||||
m := make(map[int]int)
|
||||
for i := 0; i < len(nums); i++ {
|
||||
another := target - nums[i]
|
||||
if _, ok := m[another]; ok {
|
||||
return []int{m[another], i}
|
||||
for k, v := range nums {
|
||||
if idx, ok := m[target-v]; ok {
|
||||
return []int{idx, k}
|
||||
}
|
||||
m[nums[i]] = i
|
||||
m[v] = k
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user