From f2c75c27af3bd0ed9881faaa820529730068e353 Mon Sep 17 00:00:00 2001 From: kinsozheng Date: Wed, 2 Mar 2022 22:25:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B90018=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C=20Python=E7=89=88=E6=9C=AC=20=E5=93=88?= =?UTF-8?q?=E5=B8=8C=E8=A1=A8=E6=B3=95=E8=A7=A3=E6=B3=95=20=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=E7=94=A8set()=E5=AD=98=E5=82=A8=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E7=BB=93=E6=9E=9C=E4=BD=86=E6=98=AF=E6=B5=8B=E8=AF=95=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98=20=E5=90=8C=E6=97=B6?= =?UTF-8?q?=E5=8A=A0=E5=85=A5=E4=BB=A5list()=E5=AD=98=E5=82=A8=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E7=9A=84=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0018.四数之和.md | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/problems/0018.四数之和.md b/problems/0018.四数之和.md index bad258c1..348f2f73 100644 --- a/problems/0018.四数之和.md +++ b/problems/0018.四数之和.md @@ -212,6 +212,7 @@ class Solution(object): # good thing about using python is you can use set to drop duplicates. ans = set() + # ans = [] # save results by list() for i in range(len(nums)): for j in range(i + 1, len(nums)): for k in range(j + 1, len(nums)): @@ -220,10 +221,16 @@ class Solution(object): # make sure no duplicates. count = (nums[i] == val) + (nums[j] == val) + (nums[k] == val) if hashmap[val] > count: - ans.add(tuple(sorted([nums[i], nums[j], nums[k], val]))) - else: - continue - return ans + ans_tmp = tuple(sorted([nums[i], nums[j], nums[k], val])) + ans.add(ans_tmp) + # Avoiding duplication in list manner but it cause time complexity increases + # if ans_tmp not in ans: + # ans.append(ans_tmp) + else: + continue + return list(ans) + # if used list() to save results, just + # return ans ```