내일도무사히 2021. 2. 6. 19:38

leetcode.com/problems/two-sum/

 

Two Sum - 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. Brute force

언어: C++ 

 

 

2중 for문을 이용해 풀었다. 단순하게 전체를 훑어서 맞는 값을 찾아내는 방식

 

2. map 이용

 

a + b = target

b = target - a 인 것을 이용해

배열을 훑는건 똑같지만 map에 값을 담고 map에 target - nums[i]를 뺀 값이 있는지 확인해서(조건을 충족하는 값이 있는지 확인해서) vector에 넣어준 뒤 리턴해주었다.