leetcode — word-search
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Source : https://oj.leetcode.com/problems/word-search/
*
*
* Given a 2D board and a word, find if the word exists in the grid.
*
* The word can be constructed from letters of sequentially adjacent cell,
* where "adjacent" cells are those horizontally or vertically neighboring.
* The same letter cell may not be used more than once.
*
* For example,
* Given board =
*
* [
* ["ABCE"],
* ["SFCS"],
* ["ADEE"]
* ]
*
* word = "ABCCED", -> returns true,
* word = "SEE", -> returns true,
* word = "ABCB", -> returns false.
*
*/
public class WordSearch {
public boolean search (List<String> board, String word) {
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board.get(i).length(); j++) {
char ch = board.get(i).charAt(j);
if (ch == word.charAt(0)) {
int[][] searchedFlag = new int[board.size()][board.get(i).length()];
for (int k = 0; k < board.size(); k++) {
Arrays.fill(searchedFlag[k], 0);
}
if (exist(board, i, j, word, 0, searchedFlag)) {
return true;
}
}
}
}
return false;
}
/**
* 递归就是每一次做的事情一样,所以才会自己调用自己
* 所以分析的时候可以先把一个元素做的事情列出来
* 该元素做的事情完成之后,传入下一个元素
* 恢复之前的状态
*
* @param board
* @param i
* @param j
* @param word
* @param index
* @param searchedFlag
* @return
*/
public boolean exist (List<String> board, int i, int j, String word, int index, int[][] searchedFlag) {
if (word.charAt(index) == board.get(i).charAt(j) && searchedFlag[i][j] == 0) {
searchedFlag[i][j] = 1;
if (index+1 == word.length()) {
return true;
}
// 判断当前匹配字符上、右、下、左有没有匹配
if (i > 0 && exist(board, i-1, j, word, index + 1, searchedFlag)
|| j < board.get(i).length()-1 && exist(board, i, j+1, word, index+1, searchedFlag)
|| i < board.size()-1 && exist(board, i+1, j, word, index+1, searchedFlag)
|| j > 0 && exist(board, i, j-1, word, index+1, searchedFlag)) {
return true;
}
searchedFlag[i][j] = 0;
}
return false;
}
public static void main(String[] args) {
WordSearch wordSearch = new WordSearch();
List<String> list = new ArrayList<String>(){{
add("ABCE");
add("SFCS");
add("ADEE");
}};
System.out.println(wordSearch.search(list, "ABCCED"));
System.out.println(wordSearch.search(list, "SEE"));
System.out.println(wordSearch.search(list, "ABCB"));
}
}
leetcode — word-search的更多相关文章
- [LeetCode] Word Search II 词语搜索之二
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- [LeetCode] Word Search 词语搜索
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- Leetcode: word search
July 6, 2015 Problem statement: Word Search Given a 2D board and a word, find if the word exists in ...
- LeetCode: Word Search 解题报告
Word SearchGiven a 2D board and a word, find if the word exists in the grid. The word can be constru ...
- LeetCode() Word Search II
超时,用了tire也不行,需要再改. class Solution { class TrieNode { public: // Initialize your data structure here. ...
- [leetcode]Word Search @ Python
原题地址:https://oj.leetcode.com/problems/word-search/ 题意: Given a 2D board and a word, find if the word ...
- [Leetcode] word search 单词查询
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- [LeetCode] Word Search [37]
题目 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fro ...
- leetcode Word Search 待解决?
终于搞定了这个DFS,最近这个DFS写的很不顺手,我一直以为递归这种东西只是在解重构时比较麻烦,现在看来,连最简单的返回true和false的逻辑关系都不能说one hundred present 搞 ...
- [LeetCode]Word Search 回溯
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
随机推荐
- 推荐vim学习教程--《Vim 练级手册》
非常不错的vim学习资源,讲解的简单明了,可以作为速查工具,在忘记时就翻下.地址如下: <Vim 练级手册>
- 微信跳转,手机WAP浏览器一键超级跳转微信指定页面
微信跳转,手机WAP浏览器一键超级跳转微信指定页面 这篇文章主要介绍了如何在手机浏览器wap网页中点击链接跳转到微信界面,需要的朋友可以参考下 先说第一种,最简单的唤起微信协议,weixin://主流 ...
- windows系统dos窗口全屏
第一次进入博客园 2017年12月7日 之前使用dos窗口时都输入的是简短的指令,今天突然感觉小框看着不舒服,就找了一下度娘,在这里感谢万能的百度,一鞠躬. 1.win+r打开dos命令窗口 2.cm ...
- SQL Server数据库可能遇到的报错
1.操作附加操作时报错: 可能的解决方法: 退出数据库,换Windows身份验证登录,就可以了 2.插入语句报错: 1) 2)
- 关于 Senparc.Weixin.Cache.Redis 引用的 StackExchange.Redis 版本不匹配的反馈测试
推测原因是老系统中有地方引用了旧版本的 StackExchange.Redis,原因是 StackExchange.Redis 1.2.6 版本未提供针对 .net 4.6 以上的支持,导致库引用会失 ...
- SUSE12Sp3-.NET Core 2.2.1 runtime安装
1.安装libicu依赖 1.在线安装 sudo mkdir /usr/local/dotnet #创建目录 cd /usr/local/dotnet sudo wget https://downlo ...
- Storm学习笔记 - Storm初识
Storm学习笔记 - Storm初识 1. Strom是什么? Storm是一个开源免费的分布式计算框架,可以实时处理大量的数据流. 2. Storm的特点 高性能,低延迟. 分布式:可解决数据量大 ...
- linux系统安装cdcfordb2udb
最近接触到db2数据库实时复制的解决方案InfoSphere CDC(Change Database Capture) .主要是通过读取源端的日志信息对目标端进行数据的增删改,从而尽量减少对源端资源的 ...
- android自动化必备之SDK
进入到SDK包中,通过打开SDK manager.exe即可看到SDK管理界面,可能部分童靴发现一直在加载出不来,我们需要设置代理来解决: 选择工具栏上的Tools->Options打开如下窗口 ...
- Jedis与Luttuce区别
如果你在网上搜索Redis 的Java客户端,你会发现,大多数文献介绍的都是 Jedis. 不可否认,Jedis是一个优秀的基于Java语言的Redis客户端. 但是,其不足也很明显:Jedis在实现 ...