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. ...
随机推荐
- QQ互联登录回调路径错误redirect uri is illegal(100010)
QQ互联登录设置的路径设置
- Sharepoint 2013 安装部署系列篇 第二篇 -- SQL集群安装
第一部分 系统集群安装. 第三部分 安装和配置网络负载均衡在前端web服务器 第四部分 安装和配置sharepoint 场(三层拓扑部署) 以下图片均为sharepoint 2010..由于本人的笔记 ...
- css3 transfrom使用以及其martix(矩阵)属性与其它属性的关系
写法 其属性martix与skew .scale .translate之间的关系 兼容性 : IE9+ : -ms-transform : IE9只支持2D转换 fire ...
- 9种CSS3炫酷图片展开预览展示动画特效
详细内容请点击 在线预览立即下载 这是一组共9款CSS3炫酷图片预览展示动画特效插件.css的新特性可以让我们制作出各种炫酷的动画效果.该图片预览展示动画特效就是一个很好的例子,该效果开始时图片堆叠在 ...
- 20141211—C#面向对象,封装
封装 一个private 的变量.在变量名上右键-重构-封装字段 小建议:在创建封装字段的时候,在名字前加 “_”用以区分. 封装时,下划线会自动去除 点击确定后: 应用: 赋值的时候走 set 取值 ...
- c++学习——类成员的访问权限
成员的访问权限 Public: 任何人,尤其是那些要使用这个类库的客户程序员,都能访问那个紧跟在public 后面声明的成员. 默认的package: 在同一个目录里面的文件,并且都没有明确指明它是属 ...
- COM包装(COM Wrappers)
为了实现传统的COM程序与.NET程序之间的相互调用,.NET提供了两个包装类:运行时可调用包装(runtime callable wrapper,RCW)和COM可调用包装(COM callable ...
- C++中的多态
多态性是面向对象程序设计的重要特征之一.多态性是指发出同样的消息被不同类型的对象接收时有可能导致完全不同的行为. 多态的实现方式包括以下3种:函数重载.运算符重载.虚函数. 1.运算符重载: #inc ...
- windows phone 自定义铃声
屌丝的电话是一个月都响不了几次的,无聊还是下了一个XX铃声,自娱自乐一下,google了一下实现原理,,,,,,真相啊!!!就是用了一个Task(SaveRingtoneTask),以前看的资料都没有 ...
- win7 php5.5 apache 源码安装 imagick扩展
最近公司项目有用到php 的imagick,折腾了好长时间才把扩展装上,最主要的就是最新的不一定是最合适的,最开始一直找最新包安装,一直都不成功,经过google了好长时间,终于找到一个有用的,灵机一 ...