[Python]Didn't pass & don't know why


  • 0
    F

    my first solution is quite understandable
    '''class Solution(object):
    def findDisappearedNumbers(self, nums):
    nums=sorted(list(set(nums)))
    for x in range(1, nums[-1] + 1):
    if x in nums:
    nums.remove(x)
    else:
    nums.append(x)
    return nums'''
    "run code"shows fine,but when i submit it shows " Runtime Error Message: Line 4: IndexError: list index out of range",clearly i don't think my code has this problem,could u guys tell me what's wrong?
    my second solution are here
    ''''
    return [x for x in range(1, len(nums) + 1) if x not in nums]
    '''
    still,didn't pass and shows TLE,don't know why


  • 0
    S

    So when you do nums.remove(x), the list nums is shortened. But the for loop won't stop until index x reaches the end of the range, which is out of range of the shortened nums.


Log in to reply
 

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