Why my solution with Python got Time Limit Exceeded?


  • 0
    C
    class Solution(object):
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        while len(nums)>0:
            a = nums.pop()
            if a not in nums:
                return a
            else:
                nums.remove(a)

  • 0

    Because it's only O(n^2) and that's very slow?


  • 0
    T

    It is actually O(n^2), but it could be tricky to see why. You have your outer while loop that will run len(nums) times. You also have looping structure inside of the while loop as well. Think about how "if a not in nums" would be implemented behind the scenes.


  • 0
    C

    thanks for your help!


Log in to reply
 

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