leetcode BFS】的更多相关文章

一.130  Surrounded Regions(https://leetcode.com/problems/surrounded-regions/description/) 题目: 解法: 这道题的意思是将所有被X包围的O都变为X(边缘的不算),我们可以维护一个队列,先把四周的O的位置放进队列中,并把这个位置的值变为Y.然后每次从队列中拿出一个位置,把这个位置四周为O的位置的值变为Y,再把这个位置放进队列(为什么要先变值再进队列呢?在下一道题中会说).一直到队列为空时,我们就成功地把所有直接…
Word Ladder 思路一:单向bfs, 使用visited数组记录哪些已经访问过了, 访问过的就不允许再次入队, 同时这里想到的是使用26个英文字母,枚举可能的取值, 类似brute force 思路二:双向bfs,使用两个set,这里没有使用queue,是因为需要在queue里查询,不方便. 另外,需要注意的一点是,每次遍历时,都是取size较小的来做搜索,初始时,各插入头和尾,之后每次取最小的set来拓展, 这样就实现了交替访问两个set, 是两者的高度在 l/2, 这样可以缩短一般的…
1. word ladder class Solution { public: int ladderLength(string beginWord, string endWord, unordered_set<string> &wordDict) { queue<string> q; q.push(beginWord); unordered_map<string, int> umap; umap[beginWord] = ; for (q.push(beginW…
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (2016-02-10) For more problems and solutions, you can see my LintCode repository. I'll keep updating for full summary and better solutions. See cnblogs t…
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. Example: X X X X X O O X X X O X X O X X After running your function, the bo…
In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is empty. There is at least one empty seat, and at least one person sitting. Alex wants to sit in the seat such that the distance between him and the closes…
Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 3 Output: 1 Example 2: Input: 1 / \ 2 3 / / \ 4 5 6 / 7 Output: 7 Note: You may assume the tree (i.e., the given root node) is not NULL. 这个题的思路其实跟[Lee…
There are n cities connected by m flights. Each fight starts from city u and arrives at v with a price w. Now given all the cities and fights, together with starting city src and the destination dst, your task is to find the cheapest price from src t…
Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string. Example 1: Input: S = "loveleetcode", C = 'e' Output: [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0] Note: S string leng…
You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-negative 2D map, in this map: 0 represents the obstacle can't be reached. 1 represents the ground can be walked through. The place with number bigger than…