How to improve run time of this O(n) python solution?


  • 0
    E

    So how do people get fast results?
    My solution is O(n) already, but it only beats like 40% of accepted solutions.
    Can someone point out where I could improve it?
    Or was the run time dependent on the time of day?

    class Solution(object):
        def twoSum(self, nums, target):
            data = {}
            for i, n in enumerate(nums):
                if n in data:
                    return data[n], i
                else:
                    data[target - n] = i
    

Log in to reply
 

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