From d874aaa2dd12685f54aba09201903319aa3bd183 Mon Sep 17 00:00:00 2001 From: Asterisk <44215173+casnz1601@users.noreply.github.com> Date: Sun, 16 Oct 2022 20:19:40 -0400 Subject: [PATCH] =?UTF-8?q?Update=200001.=E4=B8=A4=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 替代造成逻辑误导旧python代码 --- problems/0001.两数之和.md | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index acf2a995..7bada9af 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -152,25 +152,11 @@ class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: records = dict() - # 用枚举更方便,就不需要通过索引再去取当前位置的值 - for idx, val in enumerate(nums): - if target - val not in records: - records[val] = idx - else: - return [records[target - val], idx] # 如果存在就返回字典记录索引和当前索引 -``` - -Python (v2): - -```python -class Solution: - def twoSum(self, nums: List[int], target: int) -> List[int]: - rec = {} - for i in range(len(nums)): - rest = target - nums[i] - # Use get to get the index of the data, making use of one of the dictionary properties. - if rec.get(rest, None) is not None: return [rec[rest], i] - rec[nums[i]] = i + for index, value in enumerate(nums): + if target - value in records: + return [records[target- value], index] + records[value] = index + return [] ``` Go: