Skip to content

Commit 37b70b6

Browse files
committedDec 24, 2020
feat: missing-number
1 parent fcfad3c commit 37b70b6

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
 

‎arr.missing-number.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""
6+
268. 丢失的数字
7+
https://leetcode-cn.com/problems/missing-number/
8+
给定一个包含 [0, n] 中 n 个数的数组 nums ,找出 [0, n] 这个范围内没有出现在数组中的那个数。
9+
"""
10+
def missingNumber(self, nums: List[int]) -> int:
11+
l = len(nums)
12+
# 先求 1..n 之和,然后减去数组中所有的值,剩下的差值就是
13+
res = (l + 1) * l >> 1
14+
15+
for num in nums:
16+
res -= num
17+
18+
return res
19+
20+
21+
so = Solution()
22+
print(so.missingNumber([9,6,4,2,3,5,7,0,1]))

0 commit comments

Comments
 (0)
Please sign in to comment.