From 370535504ea0582d7b30ecbba265c7d934c516a5 Mon Sep 17 00:00:00 2001 From: sangge-rockpi <2251250136@qq.com> Date: Fri, 19 Jan 2024 20:29:48 +0800 Subject: [PATCH] update 1 --- leetcode/1.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/leetcode/1.py b/leetcode/1.py index d96e79e..8a3bda2 100644 --- a/leetcode/1.py +++ b/leetcode/1.py @@ -6,9 +6,15 @@ class Solution: def twoSum(self, nums: list[int], target: int) -> list[int]: answer = [] for i in range(len(nums) // 2 + 1): - if target - nums[i] == nums[i]: - continue - if target - nums[i] in nums: + another = target - nums[i] + + # both number in the list + if another in nums: + if nums[i] == another: + temp_list = nums + temp_list.pop(i) + if another not in temp_list: + continue answer.append(i) answer.append(nums.index(target - nums[i])) return answer