Cannot pass on th oj and got "wrong answer" but it runs good on my local


  • 2
    A
    public static void main(String[] args) {
    	String[] str = {"3","-4","+"};
    	//String[] str = {"2"};
    	System.out.println(new Evaluate_Reverse_Polish_Notation().evalRPN(str));
    }
    public int evalRPN(String[] tokens) {
    	int section=0;
        boolean flag=false;
    	int sum1=0,sum2=0;
    	for(int i=0;i<tokens.length;i++){
    		 if(tokens[i]=="+"){
    			 flag=true;
    			int j = i-1;
    			for(;tokens[j]=="#";){
    				tokens[j]="#";
    				 j--;
    			}
    			sum1 = Integer.parseInt(tokens[j]);
    			tokens[j]="#";
    			j--;
    			for(;tokens[j]=="#";){
    				 tokens[j]="#";
    				 j--;
    			    // break;
    			}
    			sum2 = Integer.parseInt(tokens[j]);
    			tokens[j]="#";
    			section = sum2 + sum1;
    			tokens[i]=String.valueOf(section);
    			/*System.out.println("1:"+section);*/
    		}else if(tokens[i]=="-"){
    			flag=true;
    			int j = i-1;
    			for(;tokens[j]=="#";){
    				tokens[j]="#";
    				 j--;
    			}
    			sum1 = Integer.parseInt(tokens[j]);
    			tokens[j]="#";
    			j--;
    			for(;tokens[j]=="#";){
    				 tokens[j]="#";
    				 j--;
    			    // break;
    			}
    			sum2 = Integer.parseInt(tokens[j]);
    			tokens[j]="#";
    			section = sum2 - sum1;
    			tokens[i]=String.valueOf(section);
    			/*System.out.println("2:"+section);*/
    		}
    		else if(tokens[i]=="*"){
    			flag=true;
    			int j = i-1;
    			for(;tokens[j]=="#";){
    				tokens[j]="#";
    				 j--;
    			}
    			sum1 = Integer.parseInt(tokens[j]);
    			tokens[j]="#";
    			j--;
    			for(;tokens[j]=="#";){
    				 tokens[j]="#";
    				 j--;
    			    // break;
    			}
    			sum2 = Integer.parseInt(tokens[j]);
    			tokens[j]="#";
    			section = sum2 * sum1;
    			tokens[i]=String.valueOf(section);
    			/*System.out.println("3:"+section);*/
    		}
    		 else if(tokens[i]=="/"){
    			    flag=true;
    				int j = i-1;
    				for(;tokens[j]=="#";){
    					tokens[j]="#";
    					 j--;
    					// break;
    				}
    				sum1 = Integer.parseInt(tokens[j]);
    				tokens[j]="#";
    				j--;
    				for(;tokens[j].equals("#");){
    					 tokens[j]="#";
    					 j--;
    				   //  break;
    				}
    				sum2 = Integer.parseInt(tokens[j]);
    				tokens[j]="#";
    				section = sum2 / sum1;
    				tokens[i]=String.valueOf(section);
    			/*	System.out.println("4:"+section);*/
    			}
    		 
    		}
    	if(flag==false){
    		section = Integer.parseInt(tokens[0]);
    	}
    /*	System.out.println(section);*/
        return section;
    }
    }

  • 0
    H

    Use equals() method instead of "==" to compare two different String can solve your problem.

    Whether all "+" refers to the same Object as what you would expect? It depends on how Strings are declared.

    I guess that the Strings in String[] token are created as follow:

    String s = new String("+");
    

    but not,

    String s = "+";
    

Log in to reply
 

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