编程语言
首页 > 编程语言> > LeetCode题解(1236):网络爬虫(Python)

LeetCode题解(1236):网络爬虫(Python)

作者:互联网

题目:原题链接(中等)

标签:广度优先搜索、深度优先搜索

解法时间复杂度空间复杂度执行用时
Ans 1 (Python) O ( N ) O(N) O(N) O ( N ) O(N) O(N)260ms (12.50%)
Ans 2 (Python)
Ans 3 (Python)

解法一:

class Solution:
    def crawl(self, startUrl: str, htmlParser: 'HtmlParser') -> List[str]:
        visited = {startUrl}
        queue = collections.deque([startUrl])
        host_name = 'http://' + startUrl.split('/')[2]

        while queue:
            now = queue.popleft()
            for new in htmlParser.getUrls(now):
                if new.startswith(host_name) and new not in visited:
                    queue.append(new)
                    visited.add(new)

        return list(visited)

标签:1236,Python,题解,Ans,queue,new,visited,startUrl
来源: https://blog.csdn.net/Changxing_J/article/details/117225154