Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
21 lines (19 loc) · 748 Bytes

File metadata and controls

21 lines (19 loc) · 748 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package ch07;
import java.util.HashMap;
import java.util.Map;
public class P7_3 {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> numsMap = new HashMap<>();
// 하나의 for 반복으로 통합
for (int i = 0; i < nums.length; i++) {
// target에서 num을 뺀 값이 있으면 정답으로 리턴
if (numsMap.containsKey(target - nums[i])) {
return new int[]{numsMap.get(target - nums[i]), i};
}
// 정답이 아니므로 다음번 비교를 위해 인덱스를 맵에 저장
numsMap.put(nums[i], i);
}
// 항상 정답이 존재하므로 널이 리턴되는 경우는 없음
return null;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.