You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
먼저 2개의 합을 구한다는 것은 하나를 골랐으면 다른 수는 자동으로 정해진다는 것이다.
즉, nums[i] 를 골랐다면 다른 수는 무조건 target - nums[i] 이어야 한다.
여기서 hashmap으로 target-nums[i]를 상수 시간 안에 구할 수 있다면 문제를 O(n)으로 풀 수 있다.
nums[i] 값을 키로 넣고 값으로 i 를 넣게 만들어주면 되는데, 앞에서 하나씩 넣으면서 돌리면 된다.
This discussion was converted from issue #1 on September 15, 2026 10:56.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Problem link
https://leetcode.com/problems/two-sum
Problem Summary
배열에서 두 개의 합이 target이 되는 두 인덱스를 구하는 문제.
Solution
O(n^2) 풀이는 너무 쉬우므로 O(n) 풀이를 보자.
먼저 2개의 합을 구한다는 것은 하나를 골랐으면 다른 수는 자동으로 정해진다는 것이다.
즉, nums[i] 를 골랐다면 다른 수는 무조건 target - nums[i] 이어야 한다.
여기서 hashmap으로 target-nums[i]를 상수 시간 안에 구할 수 있다면 문제를 O(n)으로 풀 수 있다.
nums[i] 값을 키로 넣고 값으로 i 를 넣게 만들어주면 되는데, 앞에서 하나씩 넣으면서 돌리면 된다.
Source Code
All reactions