題目摘要 給定一個數組,包含從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
.
新聞熱點
疑難解答