[Array] two sum

2021. 2. 2. 23:35프로그래밍-코딩테스트/LeetCode

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 the answer in any order.

 

 

배열과 target이라는 정수가 주어질 때, 배열의 원소 두개를 더해 target이 될 수 있다면, 몇번째 자리의 원소 두개를 더했는지 리턴하라는 문제.

아무래도 시간복잡도가 중요한 문제가 되겠다.

 

처음에는 target을 기준으로 원소를 빼서 나머지 값이 배열에 있는지를 찾아보려했지만, 그렇게 되면 결국 순회를 두번하게 되는 꼴이었다.

따라서 검색의 기준이 되는 객체를 만들었다.

 

 

 

'프로그래밍-코딩테스트 > LeetCode' 카테고리의 다른 글

[Linked List] Palindrome Linked List  (0) 2021.02.09
[Stack] Valid Parentheses  (0) 2021.02.09
[Tree] Diameter of Binary Tree  (0) 2021.02.02
[Hash Table] Single Number  (0) 2021.01.25
[Tree] Invert Binary Tree  (0) 2021.01.25