2022-12-23 00:44:40 +08:00
|
|
|
"""
|
2022-11-25 02:04:38 +08:00
|
|
|
File: leetcode_two_sum.py
|
|
|
|
Created Time: 2022-11-25
|
|
|
|
Author: Krahets (krahets@163.com)
|
2022-12-23 00:44:40 +08:00
|
|
|
"""
|
2022-11-25 02:04:38 +08:00
|
|
|
|
|
|
|
import sys, os.path as osp
|
|
|
|
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
|
|
|
from include import *
|
|
|
|
|
2022-11-25 20:12:20 +08:00
|
|
|
class SolutionBruteForce:
|
|
|
|
def twoSum(self, nums: List[int], target: int) -> List[int]:
|
|
|
|
for i in range(len(nums) - 1):
|
|
|
|
for j in range(i + 1, len(nums)):
|
|
|
|
if nums[i] + nums[j] == target:
|
|
|
|
return i, j
|
|
|
|
return []
|
|
|
|
|
|
|
|
class SolutionHashMap:
|
|
|
|
def twoSum(self, nums: List[int], target: int) -> List[int]:
|
|
|
|
dic = {}
|
|
|
|
for i in range(len(nums)):
|
|
|
|
if target - nums[i] in dic:
|
|
|
|
return dic[target - nums[i]], i
|
|
|
|
dic[nums[i]] = i
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
""" Driver Code """
|
|
|
|
if __name__ == '__main__':
|
|
|
|
# ======= Test Case =======
|
|
|
|
nums = [ 2,7,11,15 ];
|
|
|
|
target = 9;
|
|
|
|
|
|
|
|
# ====== Driver Code ======
|
|
|
|
# 方法一
|
|
|
|
res = SolutionBruteForce().twoSum(nums, target);
|
2022-11-27 04:19:16 +08:00
|
|
|
print("方法一 res =", res)
|
2022-11-25 20:12:20 +08:00
|
|
|
# 方法二
|
|
|
|
res = SolutionHashMap().twoSum(nums, target);
|
2022-11-27 04:19:16 +08:00
|
|
|
print("方法二 res =", res)
|