题目摘要 给定一个数组,包含从0, 1, 2, ..., n
n个唯一数字,从数组中找到缺少的那个。 例如, nums = [0, 1, 3]
,返回2
。
解法 1. 计算0到n的总和account = (0 + n) * (n + 1) / 2
,计算数组总和sum
,返回account - sum
2. 异或所有的索引和数据,返回
注意 1. 注意第一种解法中计算account是n与length的关系
可问问题
原题 Given an array containing n distinct numbers taken from 0, 1, 2, ..., n
, find the one that is missing from the array.
For example, Given nums = [0, 1, 3]
return 2
.
新闻热点
疑难解答