inblog logo
|
silver
    알고리즘문제풀기

    [알고리즘문제풀기] 조건 문자열

    silver's avatar
    silver
    Jun 07, 2025
    [알고리즘문제풀기] 조건 문자열
    Contents
    문제내가 작성한 정답다른 사람들의 정답

    문제

    school.programmers.co.kr
    https://school.programmers.co.kr/learn/courses/30/lessons/181934

    내가 작성한 정답

    class Solution { public int solution(String ineq, String eq, int n, int m) { if(eq.equals("!")){ return (ineq.equals("<"))?(n<m?1:0):(n>m?1:0); }else { return (ineq.equals("<"))?(n<=m?1:0):(n>=m?1:0); } } } // switch 표현식 class Solution { public int solution(String ineq, String eq, int n, int m) { return switch (ineq + eq) { case "<=" -> n <= m ? 1 : 0; case ">=" -> n >= m ? 1 : 0; case "<!" -> n < m ? 1 : 0; case ">!" -> n > m ? 1 : 0; default -> 0; }; } }

    switch

    int result = switch (color) { case "red" -> 1; case "blue" -> 2; default -> 0; };
    int result; switch (color) { case "red": result = 1; break; case "blue": result = 2; break; default: result = 0; }

    다른 사람들의 정답

    import java.util.Map; import java.util.function.BiFunction; class Solution { public int solution(String ineq, String eq, int n, int m) { Map<String, BiFunction<Integer, Integer, Boolean>> functions = Map.of( ">=", (a, b) -> a >= b, "<=", (a, b) -> a <= b, ">!", (a, b) -> a > b, "<!", (a, b) -> a < b ); return functions.get(ineq + eq).apply(n, m) ? 1 : 0; } }
    Share article

    silver

    RSS·Powered by Inblog