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 ...
随机推荐
- BZOJ2567 : 篱笆
设第$i$个区间的左端点为$a[i]$,区间长度为$len$,要覆盖的部分的长度为$all$,因为区间左端点递增,所以最优方案中它们的位置仍然递增. 对于链的情况,要满足三个条件: 1. 区间$i$可 ...
- vscode设置中文语言
https://jingyan.baidu.com/article/7e44095377c9d12fc1e2ef5b.html
- 性能之ab简单使用
ab是apache自带的性能测试工具,他所有关注的请求返回的状态码(2XX),不关心后续处理过程,所以测试时间很小,严重依赖CPU颗粒数 一.进入ab存放的目录执行./ab.其中/ab [option ...
- C#中添加log4net(日志文件)
1.先下载引用“log4net” 2.然后再App.config配置 3.添加一个LogHandler类 4.在Assemblyinfo类中添加配置的读取文件 5.运用日志文件 6.显示结果
- 马昕璐 201771010118《面向对象程序设计(java)》第十五周学习总结
第一部分:理论知识学习部分 JAR文件:将.class文件压缩打包为.jar文件后,使用ZIP压缩格式,GUI界面程序就可以直接双击图标运行. 既可以包含类文件,也可以包含诸如图像和声音这些其它类型的 ...
- A_B_Good Bye 2018_cf
A. New Year and the Christmas Ornament time limit per test 1 second memory limit per test 256 megaby ...
- CROS+node-basis+ajax
$.ajax({ url: this.baseUrl + this.restful.showDesigerViewList, type: "post", dataType: &qu ...
- Oracle expdp数据泵导出,并在文件上附加上日期格式
一.导出操作的计算机要安装Oracle Client(建议管理员版本) 二.在服务端创建目录 create directory dpdir as '目录'; 三.给目录赋权限 grant read,w ...
- ueditor编辑器多图上传为什么顺序打乱了
我上一个版本用的是ueditor1.3.6,自从1.4.2版以后,“前端上传模块统一改用webuploader”,ueditor在多图上传一直考虑漏掉了图片顺序的问题. 我的网站在用户上传图片文章的时 ...
- MSSQL事务隔离级别详解(SET TRANSACTION ISOLATION LEVEL)
控制到 Transact-SQL 的连接发出的 SQL Server 语句的锁定行为和行版本控制行为. TRANSACT-SQL 语法约定 语法 -- Syntax for SQL Server ...