【leetcode刷题笔记】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.
题解:实现一个DFS函数 private boolean canFind(char[][] board,int i,int j,String left,boolean[][] visited) ,从(i,j)出开始搜索串left,visited数组记录某个位置是否包含在当前搜索的路径中,因为每个字符只能被使用一次。
代码如下:
public class Solution {
private boolean canFind(char[][] board,int i,int j,String left,boolean[][] visited){
//if we have found one path
if(left.equals(""))
return true;
//look around point(i,j) see if we can find next char
char next = left.charAt(0);
if(i-1>=0 && !visited[i-1][j] && board[i-1][j]== next ){
visited[i-1][j] = true;
if(canFind(board, i-1, j, left.substring(1),visited))
return true;
visited[i-1][j]= false;
}
if(j-1>=0 && !visited[i][j-1] && board[i][j-1]== next ){
visited[i][j-1] = true;
if(canFind(board, i, j-1, left.substring(1),visited))
return true;
visited[i][j-1]= false;
}
if(i+1 < board.length && !visited[i+1][j] && board[i+1][j]== next ){
visited[i+1][j] = true;
if(canFind(board, i+1, j, left.substring(1),visited))
return true;
visited[i+1][j]= false;
}
if(j+1 < board[0].length && !visited[i][j+1] && board[i][j+1]== next ){
visited[i][j+1] = true;
if(canFind(board, i, j+1, left.substring(1),visited))
return true;
visited[i][j+1]= false;
}
return false;
}
public boolean exist(char[][] board, String word) {
if(word == null || word.length() == 0)
return true;
if(board.length == 0)
return false;
int m = board.length;
int n = board[0].length;
char now = word.charAt(0);
boolean[][] visited = new boolean[m][n];
//search board for our first char in word
for(int i = 0;i < m;i++){
for(int j = 0;j < n;j ++){
if(board[i][j]== now ){
visited[i][j]= true;
if(canFind(board, i, j, word.substring(1),visited))
return true;
visited[i][j]= false;
}
}
}
return false;
}
}
【leetcode刷题笔记】Word Search的更多相关文章
- 【leetcode刷题笔记】Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 【leetcode刷题笔记】Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- 【leetcode刷题笔记】Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- 刷题79. Word Search
一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- 【leetcode刷题笔记】Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
随机推荐
- 打造Android万能上拉下拉刷新框架--XRefreshView(三)
转载请注明出处:http://blog.csdn.net/footballclub/ 打造Android万能上拉下拉刷新框架–XRefreshView(一) 打造Android万能上拉下拉刷新框架–X ...
- 两个页面js方法兼容
1. a.js页面 //Js获取Url参数 function request(paras) { var url = location.href; var paraString = url.substr ...
- Hadoop 中的 ArrayWritable
虽然ArrayWritable不是接口,但貌似必须要子类去extends ArrayWritable,不能直接用ArrayWriable 否则会报下面的错误?(不是很确定) java.lang.Exc ...
- 使用OSChina代码托管管理项目(四)
本篇主要介绍使用Eclipse的Egit插件克隆远程project到本地的操作步骤 一.在Git资源库管理视图中新建一个远程资源库位置 点击红框中button进行加入 二.输入远程资源库相关信息.选择 ...
- Dual Camera Info
一个摄像头解决不了的问题,那就用两个:对于双摄你需要了解这些 http://www.chengshiluntan.com/wg/a/20160715/6ca0343f59789235c9419887f ...
- memcache原理和实际应用
Memcache是什么 Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的.眼下全世界不少人使用这个缓存项目来构建自己大负载的站点,来分担数据库的压力. 它能够应 ...
- Spring 中的Null-Safety
之前一直在某些代码中看到过使用@Nullable 标注过的注释,当时也没有在意到底是什么意思,所以这篇文章来谈谈Spring中关于Null的那些事. 在Java中不允许让你使用类型表示其null的安全 ...
- [Java]事件驱动程序设计
事件驱动模型三大要素 1)事件源:能接收外部事件的源体: 2)监听器xListener:能接收事件源通知的对象: 3)处理器Handler:用于处理事件的对象. 在Java中使用监听器对象处理事件的方 ...
- 对LCD接口的认识
LCD接口类型: 1.首先我们以传递的信号类型来区分主要有两大类:- 模拟信号: - VGA: Video Graphics Array- 数字信号 - TTL: Transistor Transis ...
- 精通 Android Data Binding
转自:https://github.com/LyndonChin/MasteringAndroidDataBinding 官方虽然已经给出了教程 - Data Binding Guide (中文版 - ...