[JAVA/프로그래머스] LV.1 추억 점수

윤소영·2023년 8월 3일
0

JAVA

목록 보기
24/41

문제

제출 답안

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을 더해줬다.

1개의 댓글

comment-user-thumbnail
2023년 8월 3일

이렇게 유용한 정보를 공유해주셔서 감사합니다.

답글 달기