import java.util.Stack;
public class Solution {
public static int solution(String str){
Stack<Integer> s = new Stack<>();
int answer = 0;
for(int i=0; i<str.length(); i++){
char ch = str.charAt(i);
if(Character.isDigit(ch)){ // 숫자일 경우
s.add(ch-48);
} else { // 숫자가 아닐 경우
int rt = s.pop();
int lt = s.pop();
if(ch == '+'){
s.add(lt+rt);
} else if(ch == '-'){
s.add(lt-rt);
} else if(ch == '/'){
s.add(lt/rt);
} else if(ch == '*'){
s.add(lt*rt);
}
}
}
answer = s.get(0); // 맨 앞의 결과가 연산의 결과가 된다.
return answer;
}
public static void main(String[] args) {
Solution T = new Solution();
String str = "352+*9-";
System.out.println(T.solution(str));
}
}
[JAVA 인프런 강의] TreeSet - K번째큰수, 중복없는 정렬 (0) |
2022.06.27 |
[JAVA 인프런 강의] 자료구조 - 교육과정설계 queue, 선수과목 체크 (0) |
2022.06.27 |
[JAVA 인프런 강의] 자료구조 - 크레인 인형뽑기(카카오) (0) |
2022.06.27 |
[JAVA 인프런 강의] 자료구조 - 올바른괄호, 괄호문자제거 (0) |
2022.06.27 |
[JAVA] 형 변환 - String, int, char, double, long, float, ASCII, 2진수 (0) |
2022.06.27 |