Leetcode 329. Longest Increasing Path in a Matrix

-题目

Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the answer does not exist, return -1.0.
Example:
Given a / b = 2.0, b / c = 3.0.
queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ? .
return [6.0, 0.5, -1.0, 1.0, -1.0 ].
The input is:
vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries , where equations.size() == values.size(), and the values are positive. This represents the equations. Return vector<double>.

-思路

简单的思路就是对每一个查询进行一次搜索,如果是在邻接矩阵中已经直接给出的值,那么我们可以返回这个值,如果是要通过几个基本元素的乘积来得出结果,那么用dfs算法来判断是否可以构成查询的这个元素。
要注意的是需要判断查询的元素是否存在于图中,也即x / x返回-1而不是1.

-代码
class Solution {
public:
    vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {
        //已经确定输入合法
        vector<double> result(queries.size()); 
        if(equations.size() != values.size()) return result; 

        set<string> nodes; 
        map<string, int> node2index; 
        for(int i = 0; i < equations.size(); i++) {
            nodes.insert(equations[i].first); 
            nodes.insert(equations[i].second); 
        }
        int n = nodes.size(); 
        vector<vector<double> > Adjmatrix(n, vector<double>(n, 0)); 
        int index = 0;
        set<string>::iterator it; 
        for(it = nodes.begin(); it != nodes.end(); it++) {
            node2index[*it] = index++; 
        }
        for(int i = 0; i < values.size(); i++) {
            Adjmatrix[node2index[equations[i].first]][node2index[equations[i].second]] = values[i]; 
            Adjmatrix[node2index[equations[i].second]][node2index[equations[i].first]] = 1 / values[i]; 
            Adjmatrix[node2index[equations[i].first]][node2index[equations[i].first]] = 1; 
            Adjmatrix[node2index[equations[i].second]][node2index[equations[i].second]] = 1; 
        }
        //构建初步的邻接图矩阵 用DFS求传递闭包
        for(int i = 0; i < queries.size(); i++) {
            //排除一些奇怪的情况
            if((node2index.find(queries[i].first) == node2index.end()) || (node2index.find(queries[i].second) == node2index.end())) {

                result[i] = -1; 
                continue; 
            }
            if(queries[i].first == queries[i].second) {
                result[i] = 1; 
                continue; 
            }
            //如果不是直接可预知的 通过DFS来找到它的值
            vector<bool> isVisited(n, 0); 
            result[i] = dfs(node2index[queries[i].first], node2index[queries[i].second], 1, isVisited, Adjmatrix); 
        }

        return result; 
    }
    double dfs(int s, int t, double res, vector<bool> isVisited, vector<vector<double> > Adjmatrix) {
        if(!isVisited[s]) {
            //如果没有访问过当前结点
            //访问当前结点
            isVisited[s] = 1; 
            //如果从s有到目标结点的路径返回
            if(Adjmatrix[s][t] != 0) {
                return res * Adjmatrix[s][t]; 
            }
            else {
                for(int i = 0; i < Adjmatrix[s].size(); i++) {
                    if(Adjmatrix[s][i] != 0) {
                        double cres = dfs(i, t, res*Adjmatrix[s][i], isVisited, Adjmatrix); 
                        if(cres != -1) return cres; 
                    }
                }
            }
        }
        return -1; 
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值