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 ...
随机推荐
- PHP序列号生成函数和字符串替换函数代码
/** * 序列号生成器 */ function snMaker($pre = '') { $date = date('Ymd'); $rand = rand(1000000,9999999); $t ...
- centos7系统下搭建docker本地镜像仓库
## 准备工作 用到的工具, Xshell5, Xftp5, docker.io/registry:latest镜像 关于docker的安装和设置加速, 请参考这篇博文centos7系统下 docke ...
- nginx配置前端代理
#user nobody;worker_processes 1; #error_log logs/error.log;#error_log logs/error.log notice;#error_l ...
- myeclipse中的HTML页面在浏览器中显示为乱码
myeclipse中的HTML页面在浏览器中显示为乱码 在通过myeclipse开发项目的过程中,如果用HTML页面书写前端,可能出现中文乱码现象,需要怎么解决呢?下面是我从网上搜的方法: 解决办法: ...
- SpringCloud 在Feign上使用Hystrix(断路由)
SpringCloud 在Feign上使用Hystrix(断路由) 第一步:由于Feign的起步依赖中已经引入了Hystrix的依赖,所以只需要开启Hystrix的功能,在properties文件中 ...
- Android进程间通信IPC
一.IPC的说明 IPC是Inter-Process Communication的缩写,含义为进程间通信或跨进程通信,是指两个进程之间进行数据交换的过程. IPC不是Android独有的,任何一个操作 ...
- Best Cow Line---POJ 3617(贪心)
FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year" competiti ...
- 基于Jmeter的thrift-RPC接口测试
根据需求,产品部分功能采用thrift-RPC协议进行接口的增.删.改.查,前期采用Junit对其进行测试,为了提高RPC接口测试的简洁化和后期的性能测试需求,打算通过Jmeter的java类测试实现 ...
- mongodb远程数据库的连接以及备份导入导出数据
环境win10; 运行cmd cd到目录mongodb的bin目录: 连接远程mongodb: 连接命令:mongo -u username -p pwd host:post/database(数据库 ...
- 文件上传控件bootstrap-fileinput的使用
1.插件下载地址:https://github.com/kartik-v/bootstrap-fileinput 2.插件的引用 需要引用jquery 需要结合bootstrap使用,即页面需要引入b ...