My Accepeted Javs solution with STACK


  • 3
    Y
    public class Solution {
        public int evalRPN(String[] tokens) {
            
    		
    		Stack<String> st = new Stack<String>();
    		for(int i=0;i<tokens.length;i++){
    			if(!isAnOperator(tokens[i]))
    			st.push(tokens[i]);
    			else{
    				int res = 0;
    				int a = Integer.parseInt(st.pop());
    				int b = Integer.parseInt(st.pop());
    				if(tokens[i].equals("*")){
    					 res =b * a;
    				}
    				if(tokens[i].equals("/")){
    					 res =b / a;
    				}
    				if(tokens[i].equals("+")){
    					 res =b + a;
    				}
    				if(tokens[i].equals("-")){
    					 res =b - a;
    				}
    				st.push(Integer.toString(res));
    
    				
    			}
    		}
    		
    		return (Integer.parseInt(st.pop()));
    	
        }
        
        	private static boolean isAnOperator(String string) {
    		// TODO Auto-generated method stub
    		if(string.equals("+") || string.equals("-") || string.equals("*") || string.equals("/"))
    			return true;
    		return false;
    	} 
    
    }

  • 2
    R

    Here is mine with Regex:

    public class Solution {
    public int evalRPN(String[] tokens){
    	Deque<String> stack = new LinkedList<String>();
    
    	for (String string:tokens)
    		if (string.matches("[0-9]+")||string.matches("-[0-9]+")){
    			stack.push(string);
    		}else{
    			int num1=Integer.parseInt(stack.pop());
    			int num2=Integer.parseInt(stack.pop());
    			
    			if (string.equals("+")){
    				stack.push(String.valueOf(num1+num2));
    			}
    			else if (string.equals("-")){
    				stack.push(String.valueOf(num2-num1));
    			}else if (string.equals("*")){
    				stack.push(String.valueOf(num2*num1));
    			}else {
    				stack.push(String.valueOf((int)(num2/num1)));
    			}
    		}		
    	
    	return Integer.parseInt(stack.pop());
    }
    

    }


  • 0
    X

    You could shorten this line of code

    if (string.matches("[0-9]+")||string.matches("-[0-9]+")){
    

    to

    if (string.matches("-*\\d+")){

  • 0
    B

    There seems no such a testcase for an invalid RPN. So, if we assume the input is always a valid RPN, we can do it in a switch.

       public int evalRPN(String[] tokens) {
            if(tokens == null || tokens.length == 0)
                return -1;
            
            Stack<Integer> stack = new Stack<>();
            for(String token: tokens)
                switch(token){
                case "+":
                    stack.push(stack.pop() + stack.pop()); break;
                case "-":
                    stack.push(-stack.pop() + stack.pop()); break;
                case "*":
                    stack.push(stack.pop() * stack.pop()); break;
                case "/":
                    int tmp = stack.pop();
                    stack.push(stack.pop() / tmp); break;
                default:
                    stack.push(Integer.valueOf(token));
                }
            return stack.pop();
        }

  • 0
    C

    There will be fewer int-string conversion if you use Integer for your linkedlist


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.