본문 바로가기
CodingTest

코딩테스트 입문 - Day 21

by Jiwon_Loopy 2025. 5. 5.
반응형

숨어있는 숫자의 덧셈 (2)


class Solution {
    public int solution(String my_string) {
        StringBuilder sb = new StringBuilder();
        int answer = 0;
        for(int i =0;i<my_string.length(); i++){
            String s = my_string.substring(i,i+1);
            try{
               int n = Integer.parseInt(s);
            }catch(Exception e){
            if(!sb.toString().equals(""))
                answer += Integer.parseInt(sb.toString());
                sb.setLength(0);
                continue;
            }
                sb.append(s);
        }
         if(!sb.toString().equals(""))
                answer += Integer.parseInt(sb.toString());
        return answer;
    }
}

안전지대


class Solution {
    int answer = 0;
    int [] di = {-1,-1,-1,0,1,1,1,0};
    int [] dj = {-1,0,1,-1,-1,0,1,1};
    public int solution(int[][] board) {
        for(int i = 0; i < board.length; i++){
            for(int j = 0; j < board[i].length; j++){
                if(board[i][j]!=1){
                    continue;
                }
                if(board[i][j] == 1){
                for(int k = 0; k < 8; k++){
                    int pi = i + di[k];
                    int pj = j + dj[k];
                    if(pi >= 0 && pi < board.length && pj >= 0 && pj < board[i].length && board[pi][pj] == 0){
                        board[pi][pj] = 2;
                    }
                }
                }
            }
        }
        
        
        for(int arr[] : board){
            for(int n : arr){
                if(n == 0){
                    answer++;
                }
            }
        }
        
        return answer;
    }
}

삼각형의 완성 조건 (2)


class Solution {
    public int solution(int[] sides) {
        int min = Math.min(sides[0], sides[1]);
        return min * 2 - 1;
    }
}

외계어 사전


class Solution {
    public int solution(String[] spell, String[] dic) {
        int cor = spell.length;
        for(String s : dic){
            int c = 0;
            for(String sp : spell){
                if(s.contains(sp)){
                    c++;
                }
                if(c == cor){
                    return 1;
                }
            }
        }
        return 2;
    }
}
 
728x90
반응형