01-25 03:45
Recent Posts
Recent Comments
Tags
- linux
- Java
- appetizer
- ์ด๋ธ์
- ์๋ฐ
- API๋ง์ผํ๋ ์ด์ค
- ์คํฝ์ค๋น
- ํ๋ก๋ณด๋ ธ
- Spring
- JOBํ๊ณ
- Naver Cloud
- ํ์ด์ฌ
- DATABASE
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- ์จ์ผ๋ํ
- ์กํ๊ณ
- python
- TSQL
- ํ์ด์๊ณต๋ชจ์
- mysql
- ict๊ณต๋ชจ์
- ICT๋ฉํ ๋ง
- ์๋์ด๋ ธ
- ICT
- ํ์ด์
- ์คํฝ๋ ํ
- RaspberryPi
- API MarketPlace ๊ธ๋ก๋ฒ ์ํฌํฐ์ฆ
- DB
- SQL
- Today
- Total
miinsun
[Algorithm]์๊ณ ๋ฆฌ์ฆ ์๋ฐ_38 ํ์์ ์ฐ์ฐ(postfix) ๋ณธ๋ฌธ
Algorithm/Java
[Algorithm]์๊ณ ๋ฆฌ์ฆ ์๋ฐ_38 ํ์์ ์ฐ์ฐ(postfix)
miinsun 2022. 1. 5. 20:04
๐ฌ ๋ฌธ์ ์ค๋ช
ํ์์ฐ์ฐ์์ด ์ฃผ์ด์ง๋ฉด ์ฐ์ฐํ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์ธ์.
๋ง์ฝ 3*(5+2)-9 ์ ํ์์ฐ์ฐ์์ผ๋ก ํํํ๋ฉด 352+*9- ๋ก ํํ๋๋ฉฐ ๊ทธ ๊ฒฐ๊ณผ๋ 12์ ๋๋ค.
๐จ ์ ์ถ๋ ฅ ์
์ ๋ ฅ - ์ฒซ ์ค์ ํ์์ฐ์ฐ์์ด ์ฃผ์ด์ง๋๋ค. ์ฐ์ฐ์์ ๊ธธ์ด๋ 50์ ๋์ง ์์ต๋๋ค.
์์ 1~9์ ์ซ์์ +, -, *, / ์ฐ์ฐ์๋ก๋ง ์ด๋ฃจ์ด์ง๋ค.
352+*9-
์ถ๋ ฅ - ์ฐ์ฐํ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
12
โ
๐ป Solution.java
import java.util.*;
public class Main {
public int solution(String s) {
int answer = 0;
Stack<String> stack = new Stack<>();
//ํ์ ๊ณ์ฐ์ -> ์ค์ ๊ณ์ฐ์์ผ๋ก ๋ฐ๊พธ๊ธฐ
for(char c : s.toCharArray()) {
if(!Character.isDigit(c)) {
int tmp = 0;
int rt = Integer.parseInt(stack.pop());
int lt = Integer.parseInt(stack.pop());
switch(c) {
case '+':
tmp = lt + rt;
break;
case '-':
tmp = lt - rt;
break;
case '*':
tmp = lt * rt;
break;
case '/':
tmp = lt / rt;
break;
}
stack.push(Integer.toString(tmp));
}
else {
stack.push(Character.toString(c));
}
}
answer = Integer.parseInt(stack.pop());
return answer;
}
public static void main(String[] args){
Main main = new Main();
Scanner sc =new Scanner(System.in);
String s = sc.next();
System.out.println(main.solution(s));
sc.close();
return ;
}
}
'Algorithm > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Algorithm]์๊ณ ๋ฆฌ์ฆ ์๋ฐ_40 ๊ณต์ฃผ ๊ตฌํ๊ธฐ (0) | 2022.01.09 |
---|---|
[Algorithm]์๊ณ ๋ฆฌ์ฆ ์๋ฐ_39 ์ ๋ง๋๊ธฐ (0) | 2022.01.09 |
[Algorithm]์๊ณ ๋ฆฌ์ฆ ์๋ฐ_37 ํฌ๋ ์ธ ์ธํ๋ฝ๊ธฐ(์นด์นด์ค) (0) | 2022.01.05 |
[Algorithm]์๊ณ ๋ฆฌ์ฆ ์๋ฐ_36 ๊ดํธ ๋ฌธ์ ์ ๊ฑฐ (0) | 2022.01.05 |
[Algorithm]์๊ณ ๋ฆฌ์ฆ ์๋ฐ_35 ์ฌ๋ฐ๋ฅธ ๊ดํธ (0) | 2022.01.05 |
Comments