내 답안
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 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;
}
}
Java
복사
다른 사람 풀이법
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;
}
}
Java
복사
class Solution {
public int solution(String ineq, String eq, int n, int m) {
boolean answer = false;
if (ineq.equals(">") && eq.equals("="))
answer = n >= m;
else if (ineq.equals("<") && eq.equals("="))
answer = n <= m;
else if (ineq.equals(">") && eq.equals("!"))
answer = n > m;
else
answer = n < m;
return answer ? 1 : 0;
}
}
Java
복사