1. 两数之和
func twoSum(nums []int, target int) []int {
var res []int = []int{0, 0}
var mem map[int]int = make(map[int]int)
for i := 0; i < len(nums); i++ {
if val, ok := mem[target-nums[i]]; ok {
res[0], res[1] = val, i
break
}
mem[nums[i]] = i
}
return res
}
注意,go的if 比较灵活,支持if语句前用1条语句进行初始化。