추억 점수
2024. 8. 27. 21:31ㆍProgrammers/Java
문제 설명
답
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를 반환한다.
'Programmers > Java' 카테고리의 다른 글
크레인 인형뽑기 게임 (0) | 2023.09.04 |
---|---|
마법의 엘리베이터 (0) | 2023.08.04 |
전국 대회 선발 고사 (0) | 2023.05.24 |
x 사이의 개수 (2) | 2023.05.17 |
qr code (2) | 2023.05.13 |