Leetcode 1236 网络爬虫: DFS/BFS搜索

Leetcode 1236 网络爬虫: DFS/BFS搜索_第1张图片

 

Leetcode 1236 网络爬虫: DFS/BFS搜索_第2张图片

Leetcode 1236 网络爬虫: DFS/BFS搜索_第3张图片

 

这个题目就是标准的DFS或者BFS,为分布式的爬虫做一个铺垫
不注意优化C++的 DFS会超时

/**
 * // This is the HtmlParser's API interface.
 * // You should not implement it, or speculate about its implementation
 * class HtmlParser {
 *   public:
 *     vector getUrls(string url);
 * };
 */

class Solution {
public:
    unordered_set res;
    string domain;
    vector crawl(string startUrl, HtmlParser htmlParser) {
        domain = "http://";
        for(int i=7;i(res.begin(),res.end());
    }

    void work(string &startUrl, HtmlParser &htmlParser){
        if(res.count(startUrl)) return;
        res.insert(startUrl);
        vector urls = htmlParser.getUrls(startUrl);
        for(auto url:urls){
            if(url.find(domain)!=-1){
                work(url,htmlParser);
            }
        }
    }
};

 

你可能感兴趣的:(算法)