On-screen text
268. Missing Number
Easy
Topics
Companies
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.
Example 1:
Input: nums = [3,0,1]
Output: 2
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.
Example 2:
Input: nums = [0,1]
Output: 2
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.
13.8K
360
360
268. Missing Number
SO GIVEN ARRAY
OF NUMS CONTAINING
N DISTINCT NUMBERS
OF 0 TO N,
RETURN THE ONLY
NUMBER IN THE
MISSING FROM THE ARRAY.
Code
Python3
Auto
class Solution:
def missingNumber(self, nums: List[int]) -> int:
result = 0
for i in range(len(nums));
result = result + i + 1
for i in nums:
result = result - i
return result
RESULTS IS GONNA
STORE THE OUTPUT.
SO FOR I
AND RANGE LENGTH
SO WE'RE GONNA
GO THROUGH AND SUBTRACT
THE TOTAL VALUE
IT CAN BE FIRST.
SO DO RESULTS EQUALS
RESULTS PLUS
I PLUS 1,
CAUSE IT'S GONNA
BE OFFSET BY 1.
AND THEN WE
WANNA GO THROUGH
AND THEN SUBTRACT
FOR I AND NUMS,
DO RESULTS EQUALS
RESULT MINUS I.
AND THEN IF
WE RETURN RESULTS,
THE FINAL VALUE
WHAT'S LEFT. SO
RETURN RESULT.