문제
제출 답안
import java.util.*;
class Solution {
public int[] solution(String[] name, int[] yearning, String[][] photo) {
int[] answer = new int[photo.length];
HashMap <String, Integer> map = new HashMap<>();
for(int i = 0; i < name.length; i++){
map.put(name[i], yearning[i]);
}
for(int i = 0; i < photo.length; i++){
for(int j = 0; j < photo[i].length; j++){
if(map.containsKey(photo[i][j]))
answer[i] += map.get(photo[i][j]);
else
answer[i] += 0;
}
}
return answer;
}
}
답안 풀이
딱 보자마자 hashmap을 써야겠다 싶었다.
map안에 없는 값은 0을 더해줘야하기 때문에 containsKey를 써서 없으면 0을 더해줬다.
이렇게 유용한 정보를 공유해주셔서 감사합니다.