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

0% completed

Vote For New Content
Mohammed Dh Abbas
My clean solution

Mohammed Dh Abbas

Jul 19, 2024

class Solution: def findNumbers(self, nums): missing = [] def swap(i, j): nums[i], nums[j] = nums[j], nums[i] for i in range(len(nums)): # nums[i] != nums[nums[i] - 1] = an item that is in the correctly positioned # while the item is miss positioned and not referring to an item that in the correctly positioned. keep swapping while nums[i] != i + 1 and nums[i] != nums[nums[i] - 1]: swap(i, nums[i] - 1) # like in 1, 2, 3, 1, 5, 3, 2, 8 for i in range(len(nums)): if nums[i] != i + 1: missing.append(i + 1) return missing

0

0

Comments
Comments

On this page