Back to course home
0% completed
Vote For New Content
My solution
Mohammed Dh Abbas
Jul 22, 2024
class Solution: def findNumbers(self, nums, k): missing_numbers = [] # swapping function def swap(i, j): nums[i], nums[j] = nums[j], nums[i] # if number not in position and number is not outside the array boundary and number does not point to number that is correctly positioned for i in range(len(nums)): while nums[i] > 0 and nums[i] != i + 1 and nums[i] - 1 < len(nums) and nums[i] != nums[nums[i] - 1]: swap(i, nums[i] - 1) # find the missing numbers wrong_numbers = set() for i in range(len(nums)): if nums[i] != i + 1: missing_numbers.append(i + 1) # add the miss-placed number to the set wrong_numbers.add(nums[i]) # if we have found all the missing k numbers just return if len(missing_numbers) == k: return missing_numbers # figure out remaining numbers i = len(nums) + 1 while len(missing_numbers) < k: if i not in wrong_numbers: missing_numbers.append(i) i += 1 return missing_numbers
0
0
Comments
Comments
On this page