Skip to content

Commit 1e8df3f

Browse files
committed
#268. Missing Number
1 parent 50a77ec commit 1e8df3f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

missing_number.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
2+
3+
# Example 1:
4+
# Input: nums = [3,0,1]
5+
# Output: 2
6+
# Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.
7+
8+
# Example 2:
9+
# Input: nums = [0,1]
10+
# Output: 2
11+
# Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.
12+
13+
# Example 3:
14+
# Input: nums = [9,6,4,2,3,5,7,0,1]
15+
# Output: 8
16+
# Explanation: n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums.
17+
18+
19+
# Constraints:
20+
21+
# n == nums.length
22+
# 1 <= n <= 104
23+
# 0 <= nums[i] <= n
24+
# All the numbers of nums are unique.
25+
26+
27+
28+
29+
class Solution:
30+
def missingNumber(self, nums: list[int]) -> int:
31+
n = len(nums)
32+
summ = (n*(n+1)//2 ) - sum(nums)
33+
return summ

0 commit comments

Comments
 (0)