I'm getting the right answer in Eclipse but not here


  • 0
    K
    Failing Test Case:
    
    Input:	["-3","9","*"]
    Output:	-3
    Expected:	-27
    
    
    But  the output on Eclipse:
    Doing -3*9
    answer is -27
    
    
    //Testing
    	public static void main(String[] args) {
    		EvaluatePRN u = new EvaluatePRN();
    		System.out.println("answer is " + u.evalRPN(args));
    	}
    
    
    
    public int evalRPN(String[] tokens) {
    		// IMPORTANT: Please reset any member data you declared, as
    		// the same Solution instance will be reused for each test case.
    		ArrayList<String> input = new ArrayList<String>();
    		if (tokens[0].length() > 1) {
    			tokens[0] = tokens[0].replace("[", "");
    			tokens[0] = tokens[0].replace("]", "");
    			String[] ns = tokens[0].split(",");
    			for (int j = 0; j < ns.length; j++) {
    				input.add(ns[j]);
    			}
    
    		}else {
    			for (int i = 0; i< tokens.length;i++){
    				input.add(tokens[i].trim());
    			}
    		}
    		
    
    		ArrayDeque<Integer> stack = new ArrayDeque<Integer>();
    
    		for (int i = 0; i < input.size(); i++) {
    			int a = 0;
    			int b = 0;
    			if (input.get(i).equals("*")) {
    				b = stack.pop();
    				a = stack.pop();
    				System.out.println("Doing " + a + "*" + b);
    				stack.push(a * b);
    			} else if (input.get(i).equals("+")) {
    				b = stack.pop();
    				a = stack.pop();
    				//System.out.println("Doing " + a + "+" + b);
    				stack.push(a + b);
    			} else if (input.get(i).equals("-")) {
    				b = stack.pop();
    				a = stack.pop();
    				//System.out.println("Doing " + a + "-" + b);
    				stack.push(a - b);
    			} else if (input.get(i).equals("/")) {
    				b = stack.pop();
    				a = stack.pop();
    			//	System.out.println("Doing " + a + "/" + b);
    				if (b == 0){
    				//	System.out.println("Cannot divide by zero");
    				}else{
    					stack.push(a / b);
    				}
    
    			} else {
    				stack.push(Integer.valueOf(input.get(i)));
    			}
    
    		}
    
    		return stack.pop();
    
    	}

  • 0
    S

    There is system output in your code might be the reason. Please tell your algorithm in some words and make comment in your code.


  • 2
    Y

    Your initialization of the List<String> input is not correct. The input String[] tokens is already an array of String.

    I guess you misunderstood the input format.


  • 0
    K

    I'm handling both formats of inputs that appear in the test cases: ["0","3","/"] and "[0,3,/]", without which i seem to get runtime errors.


  • 0
    K

    Never mind, it was the input i was reading wrong. Fixed it now :) thanks!


  • 0
    S

    I got the input format issue at first.
    The input which is given as example: ["2", "1", "+", "3", "*"], but in fact, the "[" and "]" are exclusive.


Log in to reply
 

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