diff --git "a/14_\345\210\267\351\242\230/problem_solving_01.py" "b/14_\345\210\267\351\242\230/problem_solving_01.py" new file mode 100644 index 0000000000000000000000000000000000000000..6c363483c3e8447fa9b0499cb13f7936f4222f68 --- /dev/null +++ "b/14_\345\210\267\351\242\230/problem_solving_01.py" @@ -0,0 +1,26 @@ +from typing import List + +class Solution: + + def twoSum(self, nums: List[int], target: int) -> List[int]: + """ + 两数之和 + :param nums: 提供的数组 + :param target: 目标值 + :return: 返回值 + """ + hashmap = {} # 字典不可以重复 + for index, num in enumerate(nums): # enumerate迭代返回index和value + hashmap[num] = index # 解决value重复的问题 + + print(hashmap) + for i, num in enumerate(nums): + j = hashmap.get(target - num) + if j is not None and i != j: + return [i, j] + + +if __name__ == '__main__': + s = Solution() + li = s.twoSum([2, 7, 11, 15], 9) + print(li)