737. Sentence Similarity II
来源:互联网 发布:千语淘客助手软件设置 编辑:程序博客网 时间:2024/05/21 18:11
搜索的时候,需要用dfs搜素,找到是否真的存在
class Solution {public: // using a to find b bool judge(string a, string b, map<string, set<string>>& maps, set<string>& visit) { if(a==b) return true; visit.insert(a); // a is searched set<string> temp = maps[a]; for( set<string>::iterator it = temp.begin();it!=temp.end();++it) { if(visit.find(*it)==visit.end()) { visit.insert(*it); if(judge(*it, b, maps, visit)) return true; } } return false; } bool areSentencesSimilarTwo(vector<string>& words1, vector<string>& words2, vector<pair<string, string>> pairs) { if(words1.size()!=words2.size()) return false; map<string, set<string>> maps; for(int i=0;i<pairs.size();i++) { string a = pairs[i].first; string b = pairs[i].second; if(maps.find(a)==maps.end()) { set<string> temp; temp.insert(b); maps[a] = temp; } else maps[a].insert(b); if(maps.find(b)==maps.end()) { set<string> temp; temp.insert(a); maps[b] = temp; } else maps[b].insert(a); } /* for(map<string, set<string>>::iterator it = maps.begin();it!=maps.end();++it) { cout<<it->first; set<string> temp = it->second; for (std::set<string>::iterator its=temp.begin(); its!=temp.end(); ++its) cout << " "<< *its; cout<<endl; }*/ set<string> visit; for(int i=0;i<words1.size();i++) { if(judge(words1[i], words2[i], maps, visit)==false) return false; visit.clear(); } return true; }};
阅读全文
0 0
- 737. Sentence Similarity II
- 737. Sentence Similarity II
- 737. Sentence Similarity II
- LeetCode # 737 Sentence Similarity II
- Sentence Similarity II问题及解法
- Sentence Similarity
- Leetcode算法学习日志-737 Sentence Similarity II
- 734. Sentence Similarity
- 734. Sentence Similarity
- 734. Sentence Similarity
- LeetCode | 734. Sentence Similarity
- Sentence Similarity问题及解法
- LWC 60:734. Sentence Similarity
- sentence
- Sentence
- SQL sentence!
- everyday sentence
- userful sentence
- python3 lambda 表达式以及在map函数中的应用
- Ubuntu分辨率调节
- RAID技术
- 最大公约数
- hadoop2.x 伪集群搭建
- 737. Sentence Similarity II
- 广搜(练习4题)
- Vue API简记
- 课堂总结(linux)
- python实现之构建朴实贝叶斯分类器
- 欢迎使用CSDN-markdown编辑器
- 机器学习sklearn19.0——集成学习——bagging、随机森林算法
- Javascript—基本常识
- 在win10基础上安装Ubuntu16.04双系统(双硬盘)