Zero to Hero
article thumbnail
Published 2021. 4. 2. 16:18
15. 3Sum Algorithm
 

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

 

1. Two Pointer를 사용한 풀이(내 풀이, start, index, end)

from typing import *


class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        answer = []
        nums.sort()
        # -4 -1 -1 0 1 2
        for i in range(1, len(nums) - 1):
            start, end = 0, len(nums) - 1
            while 0 <= start < i < end < len(nums):
                # print(start, i, end)
                cur_value = nums[start] + nums[i] + nums[end]
                if cur_value == 0:
                    answer.append((nums[start], nums[i], nums[end]))
                    start += 1
                elif cur_value > 0:
                    end -= 1
                elif cur_value < 0:
                    start += 1
        return list(map(lambda x:list(x),set(answer)))

 

2. Two Pointer를 사용한 풀이(최적화, index, start, end, 중복 결과 continue)

from typing import *


class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        answer = []
        nums.sort()
        # -4 -1 -1 0 1 2
        for i in range(len(nums) - 2):
            start, end = i+1, len(nums) - 1
            while start < end:
                # print(start, i, end)
                cur_value = nums[i] + nums[start] + nums[end]
                if cur_value == 0:
                    answer.append((nums[i], nums[start], nums[end]))
                    while start<end and nums[start]==nums[start+1]:
                        start+=1
                    while start<end and nums[end]==nums[end-1]:
                        end-=1
                    start+=1
                    end-=1
                elif cur_value > 0:
                    end -= 1
                elif cur_value < 0:
                    start += 1
        return list(map(lambda x:list(x),set(answer)))

profile

Zero to Hero

@Doljae

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!