📌Back-End/프로그래머스

Java 프로그래머스 코딩테스트 입문 Day 21 문자열, 사칙연산, 시뮬레이션, 2차원배열, 수학, 배열

구 일 2024. 7. 15. 20:03
728x90
반응형

 

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

프로그래머스 삼각형의 완성조건 (2) 자바

 

 

import java.util.Arrays;

/**
 * 코딩테스트 입문 Day 21 문자열, 사칙연산, 시뮬레이션, 2차원배열, 수학, 배열
 * 숨어있는 숫자의 덧셈 (2)
 */
public class Main1 {
    public static void main(String[] args) {
        System.out.println(solution("aAb1B2cC34oOp"));
        System.out.println(solution("1a2b3c4d123Z"));
    }

    public static int solution(String my_string) {
        return Arrays.stream(my_string.split("[a-zA-Z]+"))
                     .filter(e -> !e.isEmpty())
                     .mapToInt(Integer::parseInt)
                     .sum();
    }
}

 

 

안전지대

프로그래머스 안전지대 자바

 

 

/**
 * 코딩테스트 입문 Day 21 문자열, 사칙연산, 시뮬레이션, 2차원배열, 수학, 배열
 * 안전지대
 */
public class Main2 {
    public static void main(String[] args) {
        System.out.println(
                solution(
                        new int[][]{
                                {0, 0, 0, 0, 0},
                                {0, 0, 0, 0, 0},
                                {0, 0, 0, 0, 0},
                                {0, 0, 1, 0, 0},
                                {0, 0, 0, 0, 0}
                        }
                )
        );
        System.out.println(
                solution(
                        new int[][]{
                                {0, 0, 0, 0, 0},
                                {0, 0, 0, 0, 0},
                                {0, 0, 0, 0, 0},
                                {0, 0, 1, 1, 0},
                                {0, 0, 0, 0, 0}
                        }
                )
        );
        System.out.println(
                solution(
                        new int[][]{
                                {1, 1, 1, 1, 1, 1},
                                {1, 1, 1, 1, 1, 1},
                                {1, 1, 1, 1, 1, 1},
                                {1, 1, 1, 1, 1, 1},
                                {1, 1, 1, 1, 1, 1},
                                {1, 1, 1, 1, 1, 1}
                        }
                )
        );
    }

    public static int solution(int[][] board) {
        int answer = 0;
        int n = board.length;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (board[i][j] == 1) {
                    checkDanger(i, j, n, board);
                }
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (board[i][j] == 0) {
                    answer++;
                }
            }
        }

        return answer;
    }

    public static void checkDanger(int i, int j, int n, int[][] board) {
        int[] dx = {-1, 1, 0, 0, -1, 1, -1, 1};
        int[] dy = {0, 0, 1, -1, 1, 1, -1, -1};

        for (int k = 0; k < 8; k++) {
            int nextX = i + dx[k];
            int nextY = j + dy[k];

            if (nextX >= 0 && nextY >= 0 && nextX < n && nextY < n) {
                if (board[nextX][nextY] == 0) {
                    board[nextX][nextY] = -1;
                }
            }
        }
    }
}

 

 

삼각형의 완성조건 (2)

프로그래머스 삼각형의 완성조건 (2) 자바

 

 

/**
 * 코딩테스트 입문 Day 21 문자열, 사칙연산, 시뮬레이션, 2차원배열, 수학, 배열
 * 삼각형의 완성조건 (2)
 */
public class Main3 {
    public static void main(String[] args) {
        System.out.println(solution(new int[]{1, 2}));
        System.out.println(solution(new int[]{3, 6}));
        System.out.println(solution(new int[]{11, 7}));
    }

    public static int solution(int[] sides) {
        int max = Math.max(sides[0], sides[1]);
        int min = Math.min(sides[0], sides[1]);

        int maxValue = max + min;
        int minValue = max - min;

        return maxValue - minValue - 1;
    }
}

 

 

외계어 사전

프로그래머스 외계어 사전 자바

 

 

/**
 * 코딩테스트 입문 Day 21 문자열, 사칙연산, 시뮬레이션, 2차원배열, 수학, 배열
 * 외계어 사전
 */
public class Main4 {
    public static void main(String[] args) {
        System.out.println(solution(new String[]{"p", "o", "s"}, new String[]{"sod", "eocd", "qixm", "adio", "soo"}));
        System.out.println(solution(new String[]{"z", "d", "x"}, new String[]{"def", "dww", "dzx", "loveaw"}));
        System.out.println(solution(new String[]{"s", "o", "m", "d"}, new String[]{"moos", "dzx", "smm", "sunmmo", "som"}));
    }

    public static int solution(String[] spell, String[] dic) {
        boolean match = false;

        for (int i = 0; i < dic.length; i++) {
            int cnt = 0;

            for (int j = 0; j < spell.length; j++) {
                if (dic[i].contains(spell[j])) {
                    cnt++;
                }
            }

            if (cnt == spell.length) {
                match = true;
            }
        }

        return match ? 1 : 2;
    }
}

 

 

 

728x90
반응형