Python time limit

  • 0
    def twoSum(self, num, target):
            Tnum = num[::-1]
            for i in num:
                if (target - i) in Tnum :
                    Rnum = num[:]
                    del Rnum[num.index(i)]
                    return (num.index(i)+1 ,Rnum.index(target - i)+2)
            return 'Not Found'

  • 1
    if (target - i) in Tnum :

    To examine whether an element is in a list will cost linear time, which is too much for this problem.

    You can try to apply this using the dict type, which only costs constant time.

Log in to reply

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