【WordSearch】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 Solution {
public boolean exist(char[][] board, String word) {
if(word.length()==0)
return true;
if(board.length==0)
return false;
boolean[][] bol = new boolean[board.length][board[0].length];
for(int i=0;i<board.length;i++){
for(int j=0;j<board[i].length;j++){
if(board[i][j]==word.charAt(0)&&findExist(i,j,0,board,bol,word)){
return true;
}
}
}
return false;
}
private boolean findExist(int x, int y,int wa, char[][] board, boolean[][] bol, String word) {
if(wa>=word.length())
return true;
if(x<0||y<0)
return false;
if(x>=board.length||y>=board[x].length)
return false;
if(bol[x][y])
return false;
if(board[x][y]!=word.charAt(wa))
return false;
bol[x][y]=true;
boolean re =findExist(x+1,y,wa+1,board,bol,word)||findExist(x,y+1,wa+1,board,bol,word)||
findExist(x-1,y,wa+1,board,bol,word)||findExist(x,y-1,wa+1,board,bol,word);
bol[x][y]=false;
return re;
}
}
【WordSearch】Word Search的更多相关文章
- 【leetcode】Word Search
Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...
- 【leetcode】Word Search (middle)
今天开始,回溯法强化阶段. Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...
- 【leetcode】Word Search II(hard)★
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- 【Leetcode】【Medium】word search
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- 【数组】word search
题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...
- 【HDU2222】Keywords Search AC自动机
[HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- 【CF528D】Fuzzy Search(FFT)
[CF528D]Fuzzy Search(FFT) 题面 给定两个只含有\(A,T,G,C\)的\(DNA\)序列 定义一个字符\(c\)可以被匹配为:它对齐的字符,在距离\(K\)以内,存在一个字符 ...
- 【计算机视觉】Selective Search for Object Recognition论文阅读3
Selective Search for Object Recoginition surgewong@gmail.com http://blog.csdn.net/surgewong 在前 ...
- 【HDU2222】Keywords Search(AC自动机)
Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...
随机推荐
- LeetCode OJ--Median of Two Sorted Arrays ***
http://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 找两个有序数组的中位数,因为有序数组,并且复杂度要求O(lg(m+n))所以想 ...
- Linux VFS
翻译自Linux文档中的vfs.txt 介绍 VFS(Virtual File System)是内核提供的文件系统抽象层,其提供了文件系统的操作接口,可以隐藏底层不同文件系统的实现. Directir ...
- netcore3.0 webapi集成Swagger 5.0
在项目中引用Swashbuckle.AspNetCore和Swashbuckle.AspNetCore.Filters两个dll,在Startup中的ConfigureServices相关配置代码如下 ...
- POJ 3259 Wormholes 最短路+负环
原题链接:http://poj.org/problem?id=3259 题意 有个很厉害的农民,它可以穿越虫洞去他的农场,当然他也可以通过道路,虫洞都是单向的,道路都是双向的,道路会花时间,虫洞会倒退 ...
- 转载:P2P技术原理及应用(1)
转帖allen303allen的空间 作 者:金海 廖小飞 摘要:对等网络(P2P)有3种主要的组织结构:分布式哈希表(DHT)结构.树形结构.网状结构.P2P技术已 经延伸到几乎所有的网络应用领域, ...
- Android CrashHandler
package jason.android.utils; import android.content.Context; import android.content.pm.PackageInfo; ...
- 【hibernate】唯一约束 注解
唯一约束注解 单列约束和联合约束 分别如下 @Table( uniqueConstraints = { @UniqueConstraint(columnNames = "uid") ...
- 【IntelliJ idea/My/ecplise】启动项目前,修改配置JVM参数
My/ecplise下都是一样的: IDEA下:
- 【翻译自mos文章】当并行事务恢复进程在执行时,禁用并行事务恢复的方法
当并行事务恢复进程在执行时,禁用并行事务恢复的方法 How to Disable Parallel Transaction Recovery When Parallel Txn Recovery is ...
- 设计模式——介绍与工厂模式(扁平管理模式VS职业经理人模式)
本文主要对设计模式进行大概解说.特别是对工厂模式进行简明的解析: 一.设计模式的分类 创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. 结构型模式,共七种:适配器模式. ...