-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1. Two Sum.py
More file actions
32 lines (27 loc) · 830 Bytes
/
1. Two Sum.py
File metadata and controls
32 lines (27 loc) · 830 Bytes
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
# -*- coding: utf-8 -*-
# @Time : 2019/2/26 18:57
# @Author : xulzee
# @Email : xulzee@163.com
# @File : 1. Two Sum.py
# @Software: PyCharm
from typing import List
class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
# for i in range(len(nums)):
# if (target - nums[i]) in nums[i+1:]:
# return [i, i + 1 + nums[i+1:].index(target - nums[i])]
hashmap = {}
for index, num in enumerate(nums):
another_num = target - num
if another_num in hashmap:
return [hashmap[another_num], index]
hashmap[num] = index
if __name__ == '__main__':
A = [2, 7, 11, 15]
target = 9
print(Solution().twoSum(A, target))