배운점

- 달리기 경주를 풀고 나니 Map에 조금 익숙해져서 Map을 사용하였다. 조건을 제대로 안읽어서 그리움 점수가 없는 경우를 고려하지 않아 null 에러가 발생했지만 에러 처리를 해주니 코드가 정상 작동 하였다.

import java.util.*;
class Solution {
    public int[] solution(String[] name, int[] yearning, String[][] photo) {
        int[] answer = new int[photo.length];
        Map<String, Integer> score = new HashMap<String, Integer>();
        
        for(int i = 0; i < name.length; i++){
            score.put(name[i], yearning[i]);
        }
        
        for(int i = 0; i < photo.length; i++)
        {
            for(int j = 0; j < photo[i].length; j++){
                if(score.get(photo[i][j]) != null)
                    answer[i] += score.get(photo[i][j]);
            }
        }
        return answer;
    }
}

+ Recent posts