The return result is correct in local running, but not in OJ


  • 1
    L

    For test cases: equations = [ ["a", "b"], ["b", "c"] ]. values = [2.0, 3.0]. queries = [ ["a", "c"], ["b", "c"], ["a", "e"], ["a", "a"], ["x", "x"] ].
    The return result is correct in local running, but not in OJ. Is there anyone can find the bug?

    public class Solution {
        public double[] calcEquation(String[][] equations, double[] values, String[][] query) {
                   double[] res=new double[query.length];
    	        if(query.length==0) return res;
    	        Set<Tuple> set=new HashSet<Tuple>();
    	        for(int i=0;i<equations.length;i++){ 
    	            set.add(new Tuple(equations[i][0],equations[i][1],values[i]));
    	            set.add(new Tuple(equations[i][0],equations[i][0],1.0));
    	            set.add(new Tuple(equations[i][1],equations[i][1],1.0));
    	        } 
    	        for(int i=0;i<equations.length;i++){
    	            Set<Tuple> tmpSet=new HashSet<Tuple>();
    	            for(Tuple t:set){
    	                if(equations[i][1]==t.s1){
    	                	tmpSet.add(new Tuple(equations[i][0],t.s2,values[i]*t.val));
    	                }
    	            }
    	            set.addAll(tmpSet);
    	        }
    	        boolean flag=false;
    	        for(int i=0;i<query.length;i++){
    	            flag=false;
    	            for(Tuple t: set){
    	                if(flag==false && t.s1.equals(query[i][0]) && t.s2.equals(query[i][1])){
    	                    res[i]=t.val;
    	                    flag=true;
    	                }	                
    	            }
    	            if(flag==false) res[i]=-1.0;
    	            
    	        }
    	        return res;
        }
    }
    
    class Tuple{
    	    String s1;
    	    String s2;
    	    double val;
    	    Tuple(String s1, String s2, double val){
    	        this.s1=s1;
    	        this.s2=s2;
    	        this.val=val;
    	    }
        
    }
    

  • 0
    T

    add a query ["c", "a"] for test


Log in to reply
 

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