문제
내가 작성한 정답
class Solution {
public int solution(String ineq, String eq, int n, int m) {
if(ineq.equals(">")){
if(eq.equals("=")) return n>=m?1:0;
else return n>m?1:0;
}else{
if(eq.equals("=")) return n<=m?1:0;
else return n<m?1:0;
}
}
}다른 사람들의 정답
import java.util.Map;
import java.util.function.BiFunction;
class Solution {
public int solution(String ineq, String eq, int n, int m) {
// Map.of()로 불변 맵 생성 - 키: ineq+eq 문자열, 값: 비교 로직을 담은 BiFunction
Map<String, BiFunction<Integer, Integer, Boolean>> functions = Map.of(
">=", (a, b) -> a >= b, // n >= m 조건
"<=", (a, b) -> a <= b, // n <= m 조건
">!", (a, b) -> a > b, // n > m 조건
"<!", (a, b) -> a < b // n < m 조건
);
// ineq+eq 키로 해당 비교 함수를 찾아 n, m 적용 → true면 1, false면 0 반환
return functions.get(ineq + eq).apply(n, m) ? 1 : 0;
}
}
Share article