본문 바로가기
Algorithm/Programmers

[Programmers] Lv.0 / 조건 문자열 / Java

by unknownomad 2023. 10. 18.

문제

 

풀이

class Solution {
    public int solution(String ineq, String eq, int n, int m) {
        int answer = 0;
        if(ineq.equals(">")) {
            if(eq.equals("=")) {
                return n >= m ? 1 : 0;
            } else if(eq.equals("!")) {
                return n > m ? 1 : 0;
            }
        } else if(ineq.equals("<")) {
            if(eq.equals("=")) {
                return n <= m ? 1 : 0;
            } else if(eq.equals("!")) {
                return n < m ? 1 : 0;
            }
        }
        return 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;
    }
}

 

출처

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

댓글