Skip to content

Instantly share code, notes, and snippets.

@dongwooklee96
Created June 28, 2021 14:08
Show Gist options
  • Select an option

  • Save dongwooklee96/af37de96d609d6101d918db047332caf to your computer and use it in GitHub Desktop.

Select an option

Save dongwooklee96/af37de96d609d6101d918db047332caf to your computer and use it in GitHub Desktop.
problem 1.3 problem 1.3
"""
## 문제 : 1.3 두수의 합 찾기
주어진 정수형 배열에서 2개의 숫자를 선택하여, 더한 값이 특정 목표값을 만들때,
그 선택한 2개의 정수가 있는 배열의 인덱스를 반환하는 프로그램을 작성하라.
입력값 : nums = [2, 7, 10, 19] target = 9
출력값 : [0, 1]
"""
from typing import List
def solve(nums: List[int], target: int) -> List[int]:
hashtable_dict = {}
for i in range(0, len(nums)):
value = target - nums[i]
if hashtable_dict.get(value) is not None \
and hashtable_dict[value] != i:
return sorted([i, hashtable_dict[value]])
hashtable_dict[nums[i]] = i
return [-1, -1]
if __name__ == "__main__":
nums = list(map(int, input().split()))
target = int(input())
print(solve(nums, target))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment