mirror of
https://github.com/labuladong/fucking-algorithm.git
synced 2025-07-05 12:04:37 +08:00
添加python3代码
This commit is contained in:
@ -131,5 +131,33 @@ public int missingNumber(int[] nums) {
|
|||||||
<p align='center'>
|
<p align='center'>
|
||||||
<img src="../pictures/qrcode.jpg" width=200 >
|
<img src="../pictures/qrcode.jpg" width=200 >
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
======其他语言代码======
|
======其他语言代码======
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
def missingNumber(self, nums: List[int]) -> int:
|
||||||
|
#思路1,位运算
|
||||||
|
res = len(nums)
|
||||||
|
for i,num in enumerate(nums):
|
||||||
|
res ^= i^num
|
||||||
|
return res
|
||||||
|
```
|
||||||
|
|
||||||
|
```python
|
||||||
|
def missingNumber(self, nums: List[int]) -> int:
|
||||||
|
#思路2,求和
|
||||||
|
n = len(nums)
|
||||||
|
return n*(n+1)//2-sum(nums)
|
||||||
|
```
|
||||||
|
|
||||||
|
```python
|
||||||
|
def missingNumber(self, nums: List[int]) -> int:
|
||||||
|
#思路3,防止整形溢出的优化
|
||||||
|
res = len(nums)
|
||||||
|
for i,num in enumerate(nums):
|
||||||
|
res+=i-num
|
||||||
|
return res
|
||||||
|
```
|
||||||
|
|
||||||
|
事实上,在python3中不存在整数溢出的问题(只要内存放得下),思路3的优化提升并不大,不过看上去有内味了哈...
|
Reference in New Issue
Block a user