리트코드
리트코드 15 세 수의 합 (파이썬)
MC류짱
2022. 8. 6. 14:45
https://leetcode.com/problems/3sum/
[3Sum - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com](https://leetcode.com/problems/3sum/)
문제
배열을 입력받아 합으로 0을 만들 수 있는 3개의 엘리먼트를 출력하라.
풀이
def func(lst):
lst = sorted(list(lst))
result = []
l = -1
r = 0
while l < len(lst) - 2:
l += 1
for k in range(l+1, len(lst)-1):
r += 1
for i in range(k+1, len(lst)):
if lst[l] + lst[k] + lst[i] == 0 and sorted(list((lst[l], lst[k], lst[i]))) not in result:
result.append(sorted(list((lst[l], lst[k], lst[i]))))
else:
continue
return result
후기
문제에서 요구하는 값은 출력을 하지만, 일일히 하나 하나 다 비교해서 넣었기 때문에
리트코드에서는 time out이 난다.
이 방법을 부르트 포스라고 하는데....
효율적이지 않다.
실행 속도를 높여줄 방법을 찾아 다시 풀어봐야겠다.