forked from AishwaryaBarai/Leetcode-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1. Two Sum
32 lines (26 loc) · 829 Bytes
/
1. Two Sum
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
naive:
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
val = target - nums[i]
if (val in nums) and i != nums.index(val):
return i,nums.index(val)
dict:
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
dic = {}
i = 0
while i < len(nums):
val = target - nums[i]
if val in dic:
return(i, dic[val])
dic[nums[i]] = i
i +=1
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
d = {}
for i in range(len(nums)):
comp = target - nums[i]
if comp in d:
return d[comp],i
d[nums[i]] = i