399. 除法求值

Q:

在这里插入图片描述

A:

没思路,看别人思路做的,记录一下。把问题转化为图的遍历,不过好久没做过图的题了,dijikstra、floyd都忘了。好像学数据结构之后就再没看过,得补一下。。。
DFS:

class Solution {
public:
    set<string> points;     //点集
    map<pair<string,string>,double> edges;         //边集
    vector<double> res;     //结果数组
    vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
        for(int i=0;i<equations.size();++i){
            points.insert(equations[i][0]);
            points.insert(equations[i][1]);
            edges[make_pair(equations[i][0],equations[i][1])]=values[i];
            edges[make_pair(equations[i][1],equations[i][0])]=1/values[i];
        }
        for(int i=0;i<queries.size();++i){
            if (points.find(queries[i][0])==points.end() or points.find(queries[i][1])==points.end()){
                //找不到点
                res.push_back(-1);
            }
            else if(queries[i][0]==queries[i][1]){
                //形如x/x
                res.push_back(1);
            }
            else{
                set<string> visited;    //是否遍历过
                visited.insert(queries[i][0]);
                if (not dfs(queries[i][0],queries[i][1],1,visited)){
                    res.push_back(-1);
                }
            }
        }
        return res;
    }
    bool dfs(const string & cur,const string& dst,double cur_res,set<string>& visited){
        if(cur==dst){
            res.push_back(cur_res);
            return true;
        }
        for(const auto& point:points){
            if(visited.count(point)==0 and edges.count(make_pair(cur,point))>0){
                visited.insert(point);
                if(dfs(point,dst,cur_res*edges[make_pair(cur,point)],visited)){
                    return true;
                }
                visited.erase(point);
            }
        }
        return false;
    }
};

BFS:

class Solution {    //BFS
public:
    set<string> points;     //点集
    map<pair<string,string>,double> edges;         //边集
    vector<double> res;     //结果数组
    vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
        for(int i=0;i<equations.size();++i){
            points.insert(equations[i][0]);
            points.insert(equations[i][1]);
            edges[make_pair(equations[i][0],equations[i][1])]=values[i];
            edges[make_pair(equations[i][1],equations[i][0])]=1/values[i];
        }
        for(int i=0;i<queries.size();++i){
            if (points.find(queries[i][0])==points.end() or points.find(queries[i][1])==points.end()){
                //找不到点
                res.push_back(-1);
            }
            else if(queries[i][0]==queries[i][1]){
                //形如x/x
                res.push_back(1);
            }
            else{
                set<string> visited;    //是否遍历过
                queue<pair<string,double>> myqueue;
                myqueue.push(make_pair(queries[i][0],1));
                const string& dst=queries[i][1];    //目标字符串
                while(not myqueue.empty()){
                    auto cur=myqueue.front();
                    myqueue.pop();
                    if(cur.first==dst){ //成功到达dst
                        res.push_back(cur.second);
                        break;
                    }
                    visited.insert(cur.first);
                    for(const auto& point:points){
                        if(visited.count(point)==0 and edges.count(make_pair(cur.first,point))>0){
                            myqueue.push(make_pair(point,cur.second*edges[make_pair(cur.first,point)]));
                        }
                    }
                }
                if (res.size()==i){
                    res.push_back(-1);
                }
            }
        }
        return res;
    }
};
posted @ 2019-12-01 20:42  NeoZy  阅读(244)  评论(0编辑  收藏  举报