Algorithms and Data Structures/Coding Practices

AlgoExpert Three Number Sum

brightlightkim 2022. 7. 19. 15:05

def threeNumberSum(array, targetSum):
    array.sort()
    set = []
    for i in range(0, len(array)-2):
        left = i+1
        right = len(array)-1
        while(left < right):
            currentSum = array[i] + array[left] + array[right]
            if currentSum == targetSum:
                set.append([array[i], array[left], array[right]])
                left += 1
                right -= 1
            elif currentSum < targetSum:
                left += 1
            else:
                right -= 1
    return set

'Algorithms and Data Structures > Coding Practices' 카테고리의 다른 글

Kattis Sort  (0) 2022.07.20
Kattis Skru op!  (0) 2022.07.19
AlgoExpert Generate Document  (0) 2022.07.19
AlgoExpert Selection Sort  (0) 2022.07.19
AlgoExpert Cipher Encryptor  (0) 2022.07.19