1 Star 0 Fork 0

yuhang2__2/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
web-crawler.cpp 1.06 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 6年前 . Update web-crawler.cpp
// Time: O(|V| + |E|)
// Space: O(|V|)
/**
* // This is the HtmlParser's API interface.
* // You should not implement it, or speculate about its implementation
* class HtmlParser {
* public:
* vector<string> getUrls(string url);
* };
*/
class Solution {
public:
vector<string> crawl(string startUrl, HtmlParser htmlParser) {
vector<string> result = {startUrl};
unordered_set<string> lookup(result.cbegin(), result.cend());
for (int i = 0; i < result.size(); ++i) {
const auto& from_url = result[i];
const auto& name = hostname(from_url);
for (const auto& to_url: htmlParser.getUrls(from_url)) {
if (!lookup.count(to_url) && name == hostname(to_url)) {
result.emplace_back(to_url);
lookup.emplace(to_url);
}
}
}
return result;
}
private:
string hostname(const string& url) {
static const string scheme = "http://";
return url.substr(0, url.find('/', scheme.length()));
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yuhang2__2/LeetCode-Solutions.git
git@gitee.com:yuhang2__2/LeetCode-Solutions.git
yuhang2__2
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助