Golang Leetcode 268. Missing Number.go

2019-04-12 13:55:17 浏览数 (1)

版权声明:原创勿转 https://cloud.tencent.com/developer/article/1412944

思路

先计算不缺少元素的总和,减去当前的和,就是缺少的数

code

代码语言:javascript复制
func missingNumber(nums []int) int {
	total := 0
	for _, v := range nums {
		total  = v
	}
	l := len(nums)
	sum := l * (l   1) / 2
	return sum - total
}

0 人点赞