- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
Latest commit
20 lines (15 loc) · 504 Bytes
/
Copy pathexample.py
File metadata and controls
20 lines (15 loc) · 504 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
deftwo_sum(nums, target):
# Create a dictionary to store the indices of elements
index_dict= {}
forindex, valueinenumerate(nums):
complement=target-value
ifcomplementinindex_dict:
return [index_dict[complement], index]
# # Store the current number and its index in the dictionary
index_dict[value] =index
return []
# Example usage:
nums= [2, 7, 11, 15]
target=9
result=two_sum(nums, target)
print(result)