
* 문제 풀이
class Solution {
public int solution(String ineq, String eq, int n, int m) {
if (ineq.equals("<") && eq.equals("=")) {
return n <= m ? 1 : 0;
} else if (ineq.equals("<") && eq.equals("!")) {
return n < m ? 1 : 0;
} else if (ineq.equals(">") && eq.equals("=")) {
return n >= m ? 1 : 0;
} else if (ineq.equals(">") && eq.equals("!")) {
return n > m ? 1 : 0;
}
return 0;
}
}
첫 번째 조건인 if문에서 "<=" 비교, 삼항 연산자를 통해 n <= m이면 1, 아니면 0을 반환한다.
두 번째 조건인 else if에서 "<" 비교
세 번째 조건에서 ">=" 비교
네 번째 조건에서 ">" 비교
위 조건에 모두 해당하지 않을 경우 return 0을 해준다.
* 다른 풀이
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;
}
}
}
}
https://dobin0609.tistory.com/46#google_vignette
class Solution {
public int solution(String ineq, String eq, int n, int m) {
int answer = 0;
if (ineq.equals("<")) {
answer = eq.equals("=") ? (n <= m ? 1 : 0) : (n < m ? 1 : 0);
} else {
answer = eq.equals("=") ? (n >= m ? 1 : 0) : (n > m ? 1 : 0);
}
return answer;
}
}
'코테 > 프로그래머스 JAVA Lv.0' 카테고리의 다른 글
| [프로그래머스/java/Lv.0] 369게임 (0) | 2025.05.27 |
|---|---|
| [프로그래머스/java/Lv.0] 숫자 찾기 (0) | 2025.05.26 |
| [프로그래머스/java] 문자열 섞기 (0) | 2025.05.19 |
| [프로그래머스/java] 문자열 겹쳐쓰기 (0) | 2025.05.18 |
| [프로그래머스/java] 문자열의 뒤의 n글자 (0) | 2025.05.18 |