Python 1 line no extra space


  • 0
    R

    Here is my one line solution without extra space:

    class Solution(object):
        def findDisappearedNumbers(self, nums):
            """
            :type nums: List[int]
            :rtype: List[int]
            """
            return [i+1 for i in xrange(len(nums)) if (i+1) not in nums]
    

    I think time complexity should be O(n), but it is slower (52ms) than normal for-loop version (33 ms)

    Here is normal for-loop version:

    class Solution(object):
        def findDisappearedNumbers(self, nums):
            """
            :type nums: List[int]
            :rtype: List[int]
            """
            out = [] # output list
            for i in xrange(len(nums)):
                if (i+1) not in nums:
                    out.append(i+1)
            return out
    

Log in to reply
 

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