具体思路:
题目没读懂;
ump去重,并且需要判断getUrls是否是同一域名,有可能不是同一域名;
具体代码:
class Solution {
public:
vector<string>ans;
vector<string> crawl(string startUrl, HtmlParser htmlParser) {
string targetDomain=getDomain(startUrl);
unordered_set<string>vis;
vis.insert(startUrl);
ans.push_back(startUrl);
dfs(startUrl,htmlParser,targetDomain,vis);
return ans;
}
void dfs(string&curUrl,HtmlParser htmlParser,string&targetDomain,unordered_set<string>&vis){
vector<string>nextUrls=htmlParser.getUrls(curUrl);
for(auto&nextUrl:nextUrls){
if(getDomain(nextUrl)!=targetDomain)continue;
if(vis.count(nextUrl))continue;
vis.insert(nextUrl);
ans.push_back(nextUrl);
dfs(nextUrl,htmlParser,targetDomain,vis);
}
}
string getDomain(string&s){
int idx=s.find("/",7);
if(idx==-1)idx=s.size();
return s.substr(7,idx-7);
}
};