两数之和
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/two-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
- List item
func twoSum(nums []int, target int) []int {
for x, v :=range nums {
for y := x + 1; y < len(nums); y++ {
if target-v == nums[y] {
return []int {x, y}
}
}
}
return []int{}
}