-
Notifications
You must be signed in to change notification settings - Fork 4
/
1236_crawl.java
35 lines (32 loc) · 1.04 KB
/
1236_crawl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* // This is the HtmlParser's API interface.
* // You should not implement it, or speculate about its implementation
* interface HtmlParser {
* public List<String> getUrls(String url) {}
* }
*/
class Solution {
public List<String> crawl(String startUrl, HtmlParser htmlParser) {
List<String> res = new ArrayList<>();
Queue<String> queue = new LinkedList<String>();
queue.add(startUrl);
Set<String> set = new HashSet<>();
set.add(startUrl);
String hostName = getHostName(startUrl);
while(!queue.isEmpty()){
String current = queue.remove();
res.add(current);
List<String> list = htmlParser.getUrls(current);
for(String url: list){
if(!set.contains(url) && getHostName(url).equals(hostName)){
queue.add(url);
set.add(url);
}
}
}
return res;
}
private String getHostName(String url){
return url.split("/")[2];
}
}