力扣 第一题

都是用hash存储对应的值和键 每次拿当前值找有没有目标值-当前值 为键的hash已经存在了,如果已经存在了,当前的索引和hash的值就是要找到的索引  

否则就把当前的值和索引存入hash


func twoSum(number []int, target int) []int { hash := make(map[int]int) if len(number) <= 0 { return nil } for k,v:= range number { if p,ok := hash[target-v];ok { return []int{p,k} } else { hash[v] = k } } return nil }

  

func twoSum(number []int, target int) []int {
	hash := make(map[int]int)
	for i := 0; i < len(number); i++ {
		var another int = 0
		another = target - number[i] 


		if _, ok := hash[another]; ok {
			fmt.Println(another,i,hash)
			return []int{hash[another], i}
		} else {
			hash[number[i]] = i
		}

	}

	return nil
}

  

上一篇:CCS编译 报警#190-D enumerated type mixed with another type


下一篇:spark-sql 笔记