ylab 2023. 1. 25. 22:03

특정 자료구조나 알고리즘은 아니지만

 

두개의 점 혹은 두개의 가리키는 포인트를 이용하여 점이나 포인트를 옮겨가면서 결과값을 만족시키는 것

 

https://leetcode.com/problems/two-sum/

 

Two Sum - LeetCode

Two Sum - Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return

leetcode.com

def twoSum(nums, target):
    nums.sort()
    l, r = 0, len(nums)-1
    while l<r:
        if nums[l] + nums[r] > target:
            r-=1
        
        elif nums[l] + nums[r] < target:
            l+=1
        
        elif nums[l] + nums[r] == target:
            return True
    
    return False