문제
내가 작성한 정답
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