-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.py
executable file
·33 lines (29 loc) · 873 Bytes
/
1.py
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
33
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# author: lizhu
# date: 2020.02.03
# update: 2021-04-09
'''
leetcode-1 twoSum
'''
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
''' Violent enumeration '''
n = len(nums)
for i in range(n):
for j in range(i+1, n):
if nums[i] + nums[j] == target:
return [i, j]
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
''' Hash '''
hashtable = dict()
for i, num in enumerate(nums):
if target - num in hashtable:
return [hashtable[target - num], i]
hashtable[num] = i
if __name__ == "__main__":
nums = [2, 7, 11, 17]
target = 9
solution = Solution()
print("The index: {}".format(solution.twoSum(nums, target)))