Given an array
nums
containingn
distinct numbers in the range[0, n]
, return the only number in the range that is missing from the array.
給予一個陣列
nums
它包含n
個不同的數字且都落在[0, n]
之中,回傳範圍內唯一一個不在陣列中的數字。
O(1)
的額外空間複雜度和 O(n)
的時間複雜度。
O(n)
,答案就必定不需要排序。A XOR A = 0
這點,把 0 ~ n 都 XOR 起來之後,再與陣列中每個數 XOR。
LeetCode
C++