399. Evaluate Division

图像题,没觉得有什么简单的办法,貌似可以用Union Find来解。

感觉有2种思路,一种是先全部构建好每2个点的weight,然后直接遍历queires[][]来抓取答案。

一种是只构建简单的关系图,然后通过DFS来一一找寻要求的答案。

做了一会好他妈的麻烦,最后参考了(http://blog.csdn.net/yeqiuzs/article/details/52506433)

用的是第二种。

需要注意,不存在 优先于 相等 eg : x/x = -1.0, not 1.0, if x DNE in map.

public class Solution 
{
    public double[] calcEquation(String[][] equations, double[] values, String[][] queries) 
    {
        double[] res = new double[queries.length];
    
        
        Map<String,Map<String,Double>> map = new HashMap<String,Map<String,Double>>();
        
        
        
        Set<String> set = new HashSet<String>();
        for(int i = 0; i < equations.length;i++)
        {
            set.add(equations[i][0]);
            set.add(equations[i][1]);
            Map<String,Double> tempMap;
            if(!map.containsKey(equations[i][0]))
            {
                tempMap = new HashMap<String,Double>();
            }
            else
            {
                tempMap = map.get(equations[i][0]);
            }
            tempMap.put(equations[i][1],values[i]);
            map.put(equations[i][0], tempMap);
  
            
            if(!map.containsKey(equations[i][1]))
            {
                tempMap = new HashMap<String,Double>();
            }
            else
            {
                tempMap = map.get(equations[i][1]);
            }
            tempMap.put(equations[i][0],1.0/values[i]);
            map.put(equations[i][1],tempMap);

        }
        //cal
        
        
        for(int i = 0; i < queries.length;i++)
        {
            String a = queries[i][0];
            String b = queries[i][1];
            if(a.equals(b) && map.containsKey(a))
            {
                res[i] = 1.0;
                continue;
            }
            Map<String,Boolean> available = new HashMap<String,Boolean>();
            Iterator iter = set.iterator();
            while(iter.hasNext())
            {
                available.put((String)iter.next(),true);
            }
            
            double tempRes = helper(map,available,a,b,1.0);
            res[i] = tempRes;
            
            
        }
        
        return res;
        

    }
    
    public double helper(Map<String,Map<String,Double>> map, Map<String,Boolean> available,String a, String b, double base)
    {
        
        //new entry
        if(map.containsKey(a) && available.get(a))
        {
            available.put(a,false);
            Map<String,Double> tempMap = map.get(a);
            if(tempMap.containsKey(b)) return base * tempMap.get(b);
            else
            {
                Iterator iter = tempMap.keySet().iterator();
                double tempRes = -1.0;
                while(iter.hasNext() && tempRes == -1.0)
                {
                    String tempB = (String)iter.next();
                    
                    //available.put(tempB,false);
                    tempRes = helper(map,new HashMap<String,Boolean>(available),tempB,b,base * tempMap.get(tempB));
                    //available.put(tempB,true);
                    
                }
                
                if(tempRes == -1.0) return -1.0;
                else return tempRes;
            }
        }
        else
        {
            return -1.0;
        }
    }
}

考基本功的。二刷看看能不能先构建完整的图。

posted @ 2016-09-13 10:43  哇呀呀..生气啦~  阅读(586)  评论(0编辑  收藏  举报