Difficulty: Medium
Link: https://leetcode.com/problems/evaluate-division/
Problem
Explanation
- 其實這道題的核心思想就是圖,尋找兩個點在該無向圖中是否是聯通的。我在此解法中采用了hash表來存儲,提高了搜索效率,然后是DFS(深度優先遍歷查找)。
- 提交了一次wrong answer。中途遇到兩個問題:
- 標記路徑的
used[i]
必須要對稱,即使用了used[i] = true
,必須要有一個used[i] = false
與之對應。(在不符合條件的情況下正?;厮莶檎遥?/li> - 在搜索完成之后,對于沒有查找到結果的要將其設置為-1.0。
- 標記路徑的
cpp solution
class Solution {
public:
unordered_map<string, unordered_map<string, double>> hash;
unordered_map<string, bool> used;
vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {
int size = queries.size();
vector<double> res(size, 0.0);
for (int i = 0; i < equations.size(); i++) {
auto p = equations[i];
hash[p.first][p.second] = values[i];
hash[p.second][p.first] = 1 / values[i];
}
for (int i = 0; i < size; i++) {
if (hash.find(queries[i].first) == hash.end() || hash.find(queries[i].second) == hash.end()) {
res[i] = -1.0;
continue;
}
find(1.0, res, queries[i].first, queries[i].second, i);
if (res[i] == 0) {
res[i] = -1.0; // 發現第二個問題后添加的代碼
}
}
return res;
}
void find(double cur, vector<double> &res, string s, string t, int i) {
if (res[i] != 0) {
return;
}
if (hash.find(s) == hash.end() ) {
res[i] = -1.0;
return;
} else {
if (s == t) {
res[i] = cur;
return;
} else {
auto tmp = hash[s];
used[s] = true;
for (auto &p : hash[s] ) {
if (used.find(p.first) == used.end() || used[p.first] == false) {
used[p.first] = true;
find(cur * p.second, res, p.first, t, i);
used[p.first] = false;
}
}
used[s] = false; // 發現第一個問題后添加的代碼
}
}
}
};