31 lines
578 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
/*
使用哈希表
*/
func twoSum(nums []int, target int) []int {
hashTable := map[int]int{}
for i, x := range nums {
if val, ok := hashTable[target-x]; ok {
return []int{val, i}
}
hashTable[x] = i
}
return nil
}
/*
暴力枚举首先遍历整个数组之后再遍历后边的所有元素如果两个元素相加的值为target就返回这两个
下标
*/
func f1(nums []int, target int) []int {
for i, x := range nums {
for j := i + 1; j < len(nums); j++ {
if x+nums[j] == target {
return []int{i, j}
}
}
}
return nil
}