1 Star 0 Fork 0

xiangxiang/LeetCode-NOTES

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
solution.cpp 1.80 KB
一键复制 编辑 原始数据 按行查看 历史
默然 提交于 7年前 . update all algorithms.
class Solution
{
public:
double DFS(unordered_map<string, unordered_map<string, double> > &graph, unordered_map<string, bool> &visited, double res, string dividend, string &divisor)
{
if(dividend == divisor)
{
return res;
}
visited[dividend] = true;
for(unordered_map<string,double>::iterator ite = graph[dividend].begin();ite!=graph[dividend].end();ite++)
{
if(!visited[ite->first])
{
double t = DFS(graph,visited,res * ite->second,ite->first,divisor);
if(t!=-1)
return t;
}
}
return -1;
}
double calc(unordered_map<string, unordered_map<string,double> > &graph, unordered_map<string, bool> visited, pair<string,string> &querie)
{
if(graph.find(querie.first) == graph.end()
|| graph.find(querie.second) == graph.end())
return -1.0;
if(querie.first == querie.second)
return 1.0;
return DFS(graph,visited,1,querie.first,querie.second);
}
vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries)
{
unordered_map<string, unordered_map<string,double> > graph;
unordered_map<string, bool> visited;
for(int i=0;i<equations.size();i++)
{
graph[equations[i].first][equations[i].second] = values[i];
graph[equations[i].second][equations[i].first] = 1.0 / values[i];
visited[equations[i].first] = false;
visited[equations[i].second] = false;
}
vector<double> result;
for(int i=0;i<queries.size();i++)
{
result.push_back(calc(graph,visited,queries[i]));
}
return result;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xiangxiang920/LeetCode-NOTES.git
git@gitee.com:xiangxiang920/LeetCode-NOTES.git
xiangxiang920
LeetCode-NOTES
LeetCode-NOTES
master

搜索帮助