[
math
]
Leetcode 0268. Missing Number
https://leetcode.com/problems/missing-number
This problem is a simple version of the problem 645. Set Mismatch, so if you solved it previously, this one will be easy. Here you can look at my solution:
https://leetcode.com/problems/set-mismatch/discuss/1089475/python-on-timeo1-space-math-solution-explained
The idea is the following: let us sum all numbers between 1
and n
and then subtract sum of all nums
and in the end we will have exaclty number we need.
Time complexity is O(n)
, space complexity is O(1)
.
class Solution:
def missingNumber(self, nums):
return len(nums)*(len(nums)+1)//2 - sum(nums)
If you like the solution, you can upvote it on leetcode discussion section: Problem 0268