数组nums包含从0到n的所有整数,但其中缺了一个。请编写代码找出那个缺失的整数。你有办法在O(n)时间内完成吗?
注意:本题相对书上原题稍作改动
示例 1:
输入:[3,0,1]
输出:2
示例 2:
输入:[9,6,4,2,3,5,7,0,1]
输出:8
Python 解答:
1.求和
class Solution:
def missingNumber(self, nums: List[int]) -> int:
n = len(nums)
return (n+1)*n//2-sum(nums)
```‘
2.异或
```python
class Solution:
def missingNumber(self, nums: List[int]) -> int:
result = 0
for i in range(len(nums)):
result = result^nums[i]^i
return result^len(nums)
留言