mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
修改0018四数之和 Python版本 哈希表法解法 解决用set()存储返回结果但是测试报错的问题 同时加入以list()存储结果的方式
This commit is contained in:
@ -212,6 +212,7 @@ class Solution(object):
|
|||||||
|
|
||||||
# good thing about using python is you can use set to drop duplicates.
|
# good thing about using python is you can use set to drop duplicates.
|
||||||
ans = set()
|
ans = set()
|
||||||
|
# ans = [] # save results by list()
|
||||||
for i in range(len(nums)):
|
for i in range(len(nums)):
|
||||||
for j in range(i + 1, len(nums)):
|
for j in range(i + 1, len(nums)):
|
||||||
for k in range(j + 1, len(nums)):
|
for k in range(j + 1, len(nums)):
|
||||||
@ -220,10 +221,16 @@ class Solution(object):
|
|||||||
# make sure no duplicates.
|
# make sure no duplicates.
|
||||||
count = (nums[i] == val) + (nums[j] == val) + (nums[k] == val)
|
count = (nums[i] == val) + (nums[j] == val) + (nums[k] == val)
|
||||||
if hashmap[val] > count:
|
if hashmap[val] > count:
|
||||||
ans.add(tuple(sorted([nums[i], nums[j], nums[k], val])))
|
ans_tmp = tuple(sorted([nums[i], nums[j], nums[k], val]))
|
||||||
else:
|
ans.add(ans_tmp)
|
||||||
continue
|
# Avoiding duplication in list manner but it cause time complexity increases
|
||||||
return ans
|
# if ans_tmp not in ans:
|
||||||
|
# ans.append(ans_tmp)
|
||||||
|
else:
|
||||||
|
continue
|
||||||
|
return list(ans)
|
||||||
|
# if used list() to save results, just
|
||||||
|
# return ans
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user