Skip to content

Commit 99fd9d0

Browse files
committed
添加python3代码
1 parent 0b2efdc commit 99fd9d0

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

高频面试系列/消失的元素.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,33 @@ public int missingNumber(int[] nums) {
131131
<p align='center'>
132132
<img src="../pictures/qrcode.jpg" width=200 >
133133
</p>
134+
======其他语言代码======
134135

135-
======其他语言代码======
136+
137+
138+
```python
139+
def missingNumber(self, nums: List[int]) -> int:
140+
#思路1,位运算
141+
res = len(nums)
142+
for i,num in enumerate(nums):
143+
res ^= i^num
144+
return res
145+
```
146+
147+
```python
148+
def missingNumber(self, nums: List[int]) -> int:
149+
#思路2,求和
150+
n = len(nums)
151+
return n*(n+1)//2-sum(nums)
152+
```
153+
154+
```python
155+
def missingNumber(self, nums: List[int]) -> int:
156+
#思路3,防止整形溢出的优化
157+
res = len(nums)
158+
for i,num in enumerate(nums):
159+
res+=i-num
160+
return res
161+
```
162+
163+
事实上,在python3中不存在整数溢出的问题(只要内存放得下),思路3的优化提升并不大,不过看上去有内味了哈...

0 commit comments

Comments
 (0)