https://leetcode.com/problems/3sum/?envType=featured-list&envId=top-google-questions
input :
output :
조건 :
Solution explain : Solution1
in
과 같은 키워드를 쓰기에는 연산량을 많이 잡아먹는다.from typing import List
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
ret = set()
nums.sort()
for i in range(len(nums)):
j, k = i + 1, len(nums) - 1
if j >= k:
continue
while j < k:
val1, val2, val3 = nums[i], nums[j], nums[k]
temp = val1 + val2 + val3
if temp == 0:
ret.add((val1, val2, val3))
j += 1
k -= 1
continue
if temp > 0:
k -= 1
else:
j += 1
return list(ret)
# s = Solution()
# print(s.threeSum([-1,0,1,2,-1,-4,-2,-3,3,0,4]))
# print(s.threeSum([-1,0,1,2,-1,-4]))
# print(s.threeSum([0,1,1]))
# print(s.threeSum([0,0,0]))