Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Mohammed Dh Abbas
less confusing solution

Mohammed Dh Abbas

Jul 18, 2024

class Solution: def findMissingNumber(self, nums): def swap(i, j): nums[i], nums[j] = nums[j], nums[i] # try to position each element such as nums[i] == i. # the max element can not placed as nums[i] == i. # e.g. [4, 0, 3, 1] max = 4 # as result of placing all the elements you will have the max in the missing index i. for i in range(len(nums)): while nums[i] != i and nums[i] != len(nums): swap(i, nums[i]) for i in range(len(nums)): if nums[i] == len(nums): return i return len(nums)

1

0

Comments
Comments

On this page