给定一个二维面板和一个单词,找出该单词是否存在于网格中。
这个词可由顺序相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
例如,
给定 二维面板 =
[
  ['A','B','C','E'],
  ['S','F','C','S'],
  ['A','D','E','E']
]
单词= "ABCCED",-> 返回 true,
单词 = "SEE", -> 返回 true,
单词 = "ABCB", -> 返回 false。
详见:https://leetcode.com/problems/word-search/description/

Java实现:

class Solution {
public boolean exist(char[][] board, String word) {
int m = board.length;
int n = board[0].length;
boolean[][] visited = new boolean[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (dfs(board, word, 0, i, j, visited)) {
return true;
}
}
}
return false;
} public boolean dfs(char[][] board, String word, int index, int rowindex, int colindex, boolean[][] visited) {
if (index == word.length()){
return true;
}
if (rowindex < 0 || colindex < 0 || rowindex >=board.length || colindex >= board[0].length){
return false;
}
if (visited[rowindex][colindex]){
return false;
}
if (board[rowindex][colindex] != word.charAt(index)){
return false;
}
visited[rowindex][colindex] = true;
boolean res =
dfs(board, word, index + 1, rowindex - 1, colindex, visited)
|| dfs(board, word, index + 1, rowindex + 1, colindex, visited)
|| dfs(board, word, index + 1, rowindex, colindex + 1, visited)
|| dfs(board, word, index + 1, rowindex, colindex - 1, visited);
visited[rowindex][colindex] = false;
return res;
}
}

参考:https://www.cnblogs.com/springfor/p/3883942.html

079 Word Search 单词搜索的更多相关文章

  1. [LeetCode] 79. Word Search 单词搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  2. LeetCode 79. Word Search单词搜索 (C++)

    题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...

  3. Leetcode79. Word Search单词搜索

    给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字 ...

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

  5. [LeetCode] 79. Word Search 词语搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

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

  7. [LeetCode OJ] Word Search 深度优先搜索DFS

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  8. Java for LeetCode 079 Word Search

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  9. [LeetCode]题解(python):079 Word Search

    题目来源 https://leetcode.com/problems/word-search/ Given a 2D board and a word, find if the word exists ...

随机推荐

  1. 基于node.js及express实现中间件,实现post、get

    首先,当然是有必要的环境,安装node,这个我就不多说了. 依赖模块: "express": "^4.13.4", "request": & ...

  2. PS 滤镜— —扇形warp

    clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...

  3. 在Tabbed Activity(ViewPager)中切换Fragment

    我用Android Studio的向导新建了一个Tabbed Activity,里面是ViewPager样式的,有三个tabs.如下: 但是我尝试在第一个tab中设置一个按钮,打开其他tab的时候,却 ...

  4. Hadoop安装全教程 Ubuntu14.04+Java1.8.0+Hadoop2.7.6

    最近听了一个关于大数据的大牛的经验分享,在分享的最后大牛给我们一个他之前写好的关于大数据和地理应用demo.这个demo需要在Linux环境上搭建Hadoop平台.这次就简单的分享一下我关于在 Lin ...

  5. hibernate学习 六 Hibernate缓存

    缓存: 如果在集群环境下使用Hibernate时,(集群有节点A ,节点B) 当请求,发往A节点,A在数据库中修改了一条记录,然后节点B的缓存中如何实时的更新节点A修改的新数据          hi ...

  6. GBK点阵显示字库的制作和使用

    转自:http://blog.csdn.net/exbob/article/details/6539643 GBK编码共收录汉字21003个.符号883个,并提供1894个造字码位,简.繁体字融于一库 ...

  7. jquery中对于批量deferred的处理

    此代码仿照jquery源码中$.when()的实现 function test(i) { var dfd = $.Deferred(); if(i%2 == 0) { console.log(&quo ...

  8. hash tree

    http://en.wikipedia.org/wiki/Hash_list In computer science, a hash list is typically a list of hashe ...

  9. JavaEE 企业级分布式高级架构师课程

    总目录: 第一课(2018.7.10) 01 mybatis框架整体概况(2018.7.10)-

  10. solr-建立单机版的服务器

    回到之前打开的页面,刷新,wenda就出来了: 这个wenda是单机版的.