From 2d4e4314631174ce27486b2f1dc14fb45a4e406f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Mon, 19 Jun 2023 09:59:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=B8=A4=E6=95=B0=E4=B9=8B=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../problem_solving_01.py" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 "14_\345\210\267\351\242\230/problem_solving_01.py" 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 0000000..6c36348 --- /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) -- GitLab