• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Untitled a guest Jan 29th, 2020 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. class Solution:
2.     def twoSum(self, nums: List[int], target: int) -> List[int]:
3.         diff = []
4.         hashmap = {}
5.         for i in range(0,len(nums)):
6.             if nums[i] not in hashmap:
7.                 hashmap[nums[i]] = []
8.                 hashmap[nums[i]].append(i)
9.             else:
10.                 hashmap[nums[i]].append(i)
11.             diff.append(target - nums[i])
12.         j = 0
13.         for i in diff:
14.             #check equal corner case
15.             if i in hashmap:
16.                 if nums[hashmap[i]]==i:
17.                     if len(hashmap[i])>=2:
18.                         return [hashmap[i],hashmap[i]]
19.
20.                 if j!=hashmap[i]:
21.                     return [j,hashmap[i]]
22.             j+=1
23.         # for i in range(0,len(nums)):
24.         #     for j in range(i+1, len(nums)):
25.         #         if nums[i]+nums[j] == target:
26.         #             return [i,j]
27.         return []
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top