<DFS & BFS> 130 127
130. Surrounded Regions
BFS: 把所有边界上的O先换成A(标记),再深度遍历周围的点。
最后把O换成X(表示不符合要求),所有的A换回O
class Solution {
public void solve(char[][] board) {
if(board.length == 0 || board[0].length == 0) return;
int m = board.length;
int n = board[0].length;
for(int i = 0; i < m; i++){
dfs(board, i, 0);
dfs(board, i, n - 1);
}
for(int j = 1; j < n - 1; j++){
dfs(board, 0, j);
dfs(board, m - 1, j);
}
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(board[i][j] == 'O') board[i][j] = 'X';
if(board[i][j] == 'A') board[i][j] = 'O';
}
}
}
private void dfs(char[][] board, int i, int j){
if(i < 0 || i >= board.length || j < 0 || j >= board[0].length) return;
if(board[i][j] == 'X' || board[i][j] == 'A') return;
board[i][j] = 'A';
dfs(board, i - 1, j);
dfs(board, i + 1, j);
dfs(board, i, j - 1);
dfs(board, i, j + 1);
}
}
127. Word Ladder
BFS:queue,使用BFS时必须用到的队列,Set用于放置wordList。按当前
class Solution {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
Set<String> set = new HashSet<>(wordList);
Queue<String> queue = new LinkedList<>();
queue.add(beginWord);
int level = 0;
while(!queue.isEmpty()){
int size = queue.size();
for(int i = 0; i < size; i++){
String cur = queue.remove();
char[] wordUnit = cur.toCharArray();
if(cur.equals(endWord)) return level + 1;
for(int j = 0; j < cur.length(); j++){
char temp = wordUnit[j];
for(char c = 'a'; c <= 'z'; c++){
wordUnit[j] = c;
String s = new String(wordUnit);
if(set.contains(s)){
queue.add(s);
set.remove(s);
}
}
wordUnit[j] = temp;
}
}
level++;
}
return 0;
}
}
<DFS & BFS> 130 127的更多相关文章
- [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- DFS/BFS+思维 HDOJ 5325 Crazy Bobo
题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...
- 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)
[题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...
- ID(dfs+bfs)-hdu-4127-Flood-it!
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...
- HDU 4771 (DFS+BFS)
Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...
- DFS/BFS视频讲解
视频链接:https://www.bilibili.com/video/av12019553?share_medium=android&share_source=qq&bbid=XZ7 ...
- POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE
POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...
- [LeetCode]695. 岛屿的最大面积(DFS/BFS)、200. 岛屿数量(DFS/BFS待做/并差集待做)
695. 岛屿的最大面积 题目 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被 ...
- POJ2308连连看dfs+bfs+优化
DFS+BFS+MAP+剪枝 题意: 就是给你一个10*10的连连看状态,然后问你最后能不能全部消没? 思路: 首先要明确这是一个搜索题目,还有就是关键的一点就是连连看这个游戏是 ...
随机推荐
- 【Oracle】RMAN duplicate复制库
基础环境: 172.17.4.60 操作系统:Linux 6.4 数据库:Oracle11gR2 (源数据库) 172.17.4.61 操作系统:Linux 6.4 数据库:Oracle11gR2 ( ...
- [新概念英语] Lesson 12 : GOODBYE AND GOOD LUCK
Lesson 12 : GOODBYE AND GOOD LUCK New words and expressions : luck (n) 运气 例句 You're not having much ...
- springboot学习源码
springbootTest 学习源码链接 启动前,需要创建数据库表,修改自己的链接配置 create database test01; use test01; CREATE TABLE catego ...
- Type Erasure with Pokemon---swift的类型擦除
我感觉这个是swift的设计缺陷. 类型擦除:解决泛型类型作为公用类型的问题 是抽象的公用机制的一种实现方式. 1)类型擦除并不能解决类型不一致的兼容问题,只能解决类似继承一致性的兼容问题. 2)擦除 ...
- LeetCode 1291. Sequential Digits
题目 class Solution { public: int ans[10005]; vector<int> sequentialDigits(int low, int high) { ...
- centos 8 重启网络 systemctl restart network 失效的解决办法
参考: https://www.tecmint.com/set-static-ip-address-in-rhel-8/ https://www.tecmint.com/configure-netwo ...
- Elasticsearch(ES) 创建索引
欢迎关注笔者的公众号: 小哈学Java, 每日推送 Java 领域干货文章,关注即免费无套路附送 100G 海量学习.面试资源哟!! 个人网站: https://www.exception.site/ ...
- Visual Studio 2017使用ODT 连接Oracle 数据库出现异常
2019.5.23 更新 突然发现原来是是sqlnet.ora在搞鬼,只要将SQLNET.AUTHENTICATION_SERVICES=(nts) 改为 SQLNET.AUTHENTICATION ...
- 开源工作流引擎 Workflow Core 的研究和使用教程
目录 开源工作流引擎 Workflow Core 的研究和使用教程 一,工作流对象和使用前说明 二,IStepBuilder 节点 三,工作流节点的逻辑和操作 容器操作 普通节点 事件 条件体和循环体 ...
- python 错误、调试、单元测试、文档测试
错误分为程序的错误和由用户错误的输入引起的错误,此外还有因为各种各样意外的情况导致的错误,比如在磁盘满的时候写入.从网络爬取东西的时候,网络断了.这类错误称为异常 错误处理 参考链接:https:// ...