Programmers/Java
추억 점수
Anoi
2024. 8. 27. 21:31
문제 설명
답
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import java.util.*;
class Solution {
public int[] solution(String[] name, int[] yearning, String[][] photo) {
int[] result = new int[photo.length];
HashMap<String, Integer> map = new HashMap<String, Integer>();
for(int i=0; i<name.length; i++) {
map.put(name[i], yearning[i]);
}
for(int i=0; i<photo.length; i++) {
int sum = 0;
for(int j=0; j<photo[i].length; j++) {
if(map.containsKey(photo[i][j])) {
sum += map.get(photo[i][j]);
}
}
result[i] = sum;
}
return result;
}
}
|
cs |
1. map을 만들어 name과 yearning의 값을 차례대로 넣는다.
2. photo를 탐색하기 위한 이중 for문을 만들고 photo의 이름이 map에 있는지 조건문을 건다.
3. 있다면 해당 name에 해당하는 yearning의 값을 더해준다.
4. 나온 합산값을 result 배열에 넣는다.
contrainsKey
: 맵에서 인자로 보낸 키가 있으면 true 없으면 false를 반환한다.