my python solution, neat with explanation

  • 0

    First, we initialized a list called rank that contains n positive number : you can either use range or just set these n numbers as 1. But I thank the first is better, because it's easier for us to find the index.

    Then, we traverse the num in list nums, and set the number in rank with index num to be a negative value, that is, this number has been "visited".

    At last, we just need to return the positive value in rank since it has not been visited, which means it doesn't appear in nums. And such positive values are just the indexes that to be returned.

    class Solution(object):
        def findDisappearedNumbers(self, nums):
            :type nums: List[int]
            :rtype: List[int]
            n = len(nums)
            rank = range(0,n+1)
            for num in nums:
                rank[num] = -1
            return [j for j in rank if j>0]

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.