Python dictionary and two-pointer solutions.


  • 4
    C
    # dictionary 
    def twoSum1(self, nums, target):
        dic = {}
        for i, num in enumerate(nums):
            if target-num in dic:
                return (dic[target-num]+1, i+1)
            dic[num] = i
     
    # two-pointer       
    def twoSum(self, nums, target):
        nums = enumerate(nums)
        nums = sorted(nums, key=lambda x:x[1])
        l, r = 0, len(nums)-1
        while l < r:
            if nums[l][1]+nums[r][1] == target:
                return sorted([nums[l][0]+1, nums[r][0]+1])
            elif nums[l][1]+nums[r][1] < target:
                l += 1
            else:
                r -= 1

  • 0
    L

    Does two-pointer solution have any advantage?


  • 0
    C

    It is an idea to solve a category of problems, like 3sum, 4sum etc.


Log in to reply
 

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