Advertisement
sweet1cris

Untitled

Nov 25th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.71 KB | None | 0 0
  1. public class Solution {
  2.     public int evalRPN(String[] tokens) {
  3.         Stack<Integer> s = new Stack<Integer>();
  4.         String operators = "+-*/";
  5.         for(String token : tokens){
  6.             if(!operators.contains(token)){
  7.                 s.push(Integer.valueOf(token));
  8.                 continue;
  9.             }
  10.  
  11.             int a = s.pop();
  12.             int b = s.pop();
  13.             if(token.equals("+")) {
  14.                 s.push(b + a);
  15.             } else if(token.equals("-")) {
  16.                 s.push(b - a);
  17.             } else if(token.equals("*")) {
  18.                 s.push(b * a);
  19.             } else {
  20.                 s.push(b / a);
  21.             }
  22.         }
  23.         return s.pop();
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement