Missing Number
Easy
LC #268
bit manipulationxormathNot attempted yet
nums holds n distinct numbers taken from the range
0..n (that's n + 1 candidates), so exactly one is
missing. Return it.
Aim for O(n) time and O(1) extra space.
Example 1
Input: nums = [3, 0, 1]
Output: 2
Example 2
Input: nums = [0, 1]
Output: 2
n = 2, so the range is 0..2 and 2 is missing.
Example 3
Input: nums = [9, 6, 4, 2, 3, 5, 7, 0, 1]
Output: 8
Constraints
- 1 ≤ n ≤ 10⁵
- all values distinct, each in 0..n