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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. Leetcode: word search

    July 6, 2015 Problem statement: Word Search Given a 2D board and a word, find if the word exists in ...

  4. LeetCode: Word Search 解题报告

    Word SearchGiven a 2D board and a word, find if the word exists in the grid. The word can be constru ...

  5. LeetCode() Word Search II

    超时,用了tire也不行,需要再改. class Solution { class TrieNode { public: // Initialize your data structure here. ...

  6. [leetcode]Word Search @ Python

    原题地址:https://oj.leetcode.com/problems/word-search/ 题意: Given a 2D board and a word, find if the word ...

  7. [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 ...

  8. [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 ...

  9. leetcode Word Search 待解决?

    终于搞定了这个DFS,最近这个DFS写的很不顺手,我一直以为递归这种东西只是在解重构时比较麻烦,现在看来,连最简单的返回true和false的逻辑关系都不能说one hundred present 搞 ...

  10. [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 ...

随机推荐

  1. 如何删除github上的某个文件夹

    在github上只能删除仓库,却无法删除文件夹或文件, 所以只能通过命令来解决 首先进入你的master文件夹下, Git Bash Here ,打开命令窗口 $ git –help 帮助命令 $ g ...

  2. VMware手动添加centos7硬盘图文操作及分区超详细

    先设置虚拟机 启动的虚拟机,新关机再设置 1.选择指定虚拟机,点击硬盘 2.虚拟机设置,点击左下角“添加” 3.硬件类型选择硬盘,点击下一步 4.添加硬件向导默认就行,下一步 5.选择磁盘,默认选中, ...

  3. wpf 的各个template

    --转载 在使用TabControl.ListView.Menu.TreeView的时候被各种Template搞得头昏眼花,决心把这个问题搞清楚,究竟什么时候该用什么Template?这是个麻烦的问题 ...

  4. KindEditor富文本编辑器, 从客户端中检测到有潜在危险的 Request.Form 值

    在用富文本编辑器时经常会遇到的问题是asp.net报的”检测到有潜在危险的 Request.Form 值“一般的解法是在aspx页面   page  标签中加上 validaterequest='fa ...

  5. STS(Spring Tool Suite)下SSM(Spring+SpringMVC+Mybatis)框架搭建(二)

    继完成controller配置并使用controller实现页面跳转,现连接数据库进行登录. 在SSM框架中,使用Mybatis与数据库连接,因此需要配置关于mybatis的配置. 废话少说直接开始: ...

  6. CentOS7 防火墙(firewall)的操作命令(转)

    安装:yum install firewalld 1.firewalld的基本使用 启动: systemctl start firewalld 查看状态: systemctl status firew ...

  7. activiti数据库表结构剖析

    1.结构设计 1.1.    逻辑结构设计 Activiti使用到的表都是ACT_开头的. ACT_RE_*: ’RE’表示repository(存储),RepositoryService接口所操作的 ...

  8. 十五、过滤器(Filter)

    过滤器(Filter) 过滤器概述 1 什么是过滤器 过滤器JavaWeb三大组件之一,它与Servlet很相似!不它过滤器是用来拦截请求的,而不是处理请求的. 当用户请求某个Servlet时,会先执 ...

  9. 解决Visual Studio 加载符号卡死情况

    VS 加载符号 过慢或卡死的情况都可以用这种方法 打开VS的[工具]-[选项]-[调试]-[符号], 如下图所示: 1. 先取消勾选 ”Microsoft符号服务器” 2. 再点击 “清空符号缓存” ...

  10. 连接池c3p0 ,Proxool ,Druid ,Tomcat Jdbc Pool对比测试

    这次所要做的测试是比较几种我们常用的数据库连接池的性能,他们分别是:c3p0 ,Proxool ,Druid ,Tomcat Jdbc Pool这四种,测试将采用统一的参数配置力求比较“公平”的体现统 ...