代码拉取完成,页面将自动刷新
// 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()));
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。