Skip to content

Commit a6e5bca

Browse files
committed
Missing Number
1 parent 8163e2a commit a6e5bca

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Missing_Number.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Given an array containing n distinct numbers taken from 0, 1, 2, ..., n,
2+
# find the one that is missing from the array.
3+
#
4+
# Example 1:
5+
#
6+
# Input: [3,0,1]
7+
# Output: 2
8+
# Example 2:
9+
#
10+
# Input: [9,6,4,2,3,5,7,0,1]
11+
# Output: 8
12+
13+
14+
class Solution:
15+
def missingNumber(self, nums):
16+
n = len(nums)
17+
expectedSum = n * (n + 1) // 2
18+
19+
actualSum = 0
20+
for num in nums:
21+
actualSum += num
22+
23+
return expectedSum - actualSum

0 commit comments

Comments
 (0)