Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int evalRPN(String[] tokens) {
- int a, b;
- Stack<Integer> S = new Stack<Integer>();
- for (String s : tokens) {
- if (s.equals("+")) {
- S.add(S.pop() + S.pop());
- } else if (s.equals("/")) {
- b = S.pop();
- a = S.pop();
- S.add(a / b);
- } else if (s.equals("*")) {
- S.add(S.pop() * S.pop());
- } else if (s.equals("-")) {
- b = S.pop();
- a = S.pop();
- S.add(a - b);
- } else {
- S.add(Integer.parseInt(s));
- }
- }
- return S.pop();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement