LintCode-Word Search II
Given a matrix of lower alphabets and a dictionary. Find all words in the dictionary that can be found in the matrix. A word can start from any position in the matrix and go left/right/up/down to the adjacent position.
Example
Given matrix:
doaf
agai
dcan
and dictionary:
{"dog", "dad", "dgdg", "can", "again"}
public class Solution {
/**
* @param board: A list of lists of character
* @param words: A list of string
* @return: A list of string
*/
public ArrayList<String> wordSearchII(char[][] board, ArrayList<String> words) {
ArrayList<String> res = new ArrayList<String>();
if (board.length==0) return res;
int rowNum = board.length;
if (board[0].length==0) return res;
int colNum = board[0].length;
for (int i=0;i<words.size();i++){
String word = words.get(i);
if (word.length()==0) continue;
for (int j=0;j<rowNum;j++){
boolean valid = false;
for (int k=0;k<colNum;k++)
if (board[j][k]==word.charAt(0)){
boolean[][] visited = new boolean[rowNum][colNum];
for (int p = 0;p<rowNum;p++)
Arrays.fill(visited[p],false);
valid = isValidWord(board,visited,word,0,j,k);
if (valid){
res.add(word);
break;
}
}
if (valid) break;
}
}
return res;
}
public boolean isValidWord(char[][] board, boolean[][] visited, String word, int pos, int x, int y){
if (x<0 || x>=board.length || y<0 || y>=board[0].length) return false;
if (word.charAt(pos)!=board[x][y] || visited[x][y]) return false;
if (pos==word.length()-1)
return true;
visited[x][y] = true;
if (isValidWord(board,visited,word,pos+1,x+1,y) || isValidWord(board,visited,word,pos+1,x-1,y) || isValidWord(board,visited,word,pos+1,x,y+1) || isValidWord(board,visited,word,pos+1,x,y-1))
return true;
else {
visited[x][y]=false;
return false;
}
}
}
LintCode-Word Search II的更多相关文章
- Leetcode之回溯法专题-212. 单词搜索 II(Word Search II)
Leetcode之回溯法专题-212. 单词搜索 II(Word Search II) 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单 ...
- leetcode 79. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
- 【leetcode】212. Word Search II
Given an m x n board of characters and a list of strings words, return all words on the board. Each ...
- [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 ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- 212. Word Search II
题目: Given a 2D board and a list of words from the dictionary, find all words in the board. Each word ...
- 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] 212. 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] 212. Word Search II 词语搜索 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 II
超时,用了tire也不行,需要再改. class Solution { class TrieNode { public: // Initialize your data structure here. ...
随机推荐
- GetImage
check路径是是否有效,网络访问地址.notfound查找不到,httpstatuscode枚举数较多,根据自己想要的设置就好了. public bool checkValid(string pat ...
- struts2框架的第一个程序
helloworld.jsp在base目录下 <a href="${pageContext.request.contextPath }/base/HelloWorldAction.ac ...
- AspxGridView ComboBoxComlum列数据联动
第1步: 页面放置AspxGridView控件, 设置列ComboBox1, ComboBox2列, 拟通过ComboBox1列更新联动ComboBox2列. 两个数据列均为"ComboBo ...
- Android高级音频应用
说到音频应用,首先想到的就是音乐播放器.有些播放器可以播放流媒体,有些可以播放本地音乐文件.随着Android平台的演变,需要更多高级的音频API.好在谷歌新增了这方面的API,支持低延迟的音频流媒体 ...
- About Restful Web Api Something.
这种轻量级的服务架构目前来说还是比较流行的,比如微信的公众平台的接口开发就是一个很好的案例,提到restful那么他到底是一个什么样的东西? REST(Representational State T ...
- C# lock用法
当我们使用线程的时候,效率最高的方式当然是异步,即各个线程同时运行,其间不相互依赖和等待.但当不同的线程都需要访问某个资源的时候,就需要同步机制了,也就是说当对同一个资源进行读写的时候,我们要使该资源 ...
- UI1_UITableViewHomeWork
// // AppDelegate.m // UI1_UITableViewHomeWork // // Created by zhangxueming on 15/7/14. // Copyrigh ...
- extern 数组
最近比较关注C++对象的Linkage类型,然后今天突然想起extern数组这个奇葩的东西,稍微折腾了一下,顺手写个随笔. 首先在cpp中定义几个数组: ,,,,}; ,,,,}; ,,,,}; 然后 ...
- C语言1-100连加,求质数,算瑞年检测字母大小写,登录系统
#include <stdio.h> void test(){//1+2+3+4+.....+100 int a,b; a=0; b=0; for ( ; a<=100; a++) ...
- 水仙花数 java 实现
题目描述: 春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的:“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3+3^3.现在要求 ...