79. Word Search java solutions

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 =

[
  [‘A‘,‘B‘,‘C‘,‘E‘],
  [‘S‘,‘F‘,‘C‘,‘S‘],
  [‘A‘,‘D‘,‘E‘,‘E‘]
]

word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.

Subscribe to see which companies asked

 1 public class Solution {
 2     public boolean exist(char[][] board, String word) {
 3         if(word == null || word.length() == 0) return true;
 4         boolean[][] visit = new boolean[board.length][board[0].length];
 5         char[] words = word.toCharArray();
 6         for(int i = 0; i < board.length; i++){
 7             for(int j = 0; j < board[0].length; j++){
 8                 if(BFS(board,visit,words,0,i,j)) return true;
 9             }
10         }
11         return false;
12     }
13
14     public boolean BFS(char[][] board,boolean[][] visit, char[] word, int len, int row, int col){
15         if(len == word.length){
16             return true;
17         }
18         if(row < 0 || row == board.length || col < 0 || col == board[row].length) return false;
19         if(board[row][col] != word[len]) return false;
20         if(!visit[row][col]){
21         visit[row][col] = true;
22         boolean tmp = BFS(board,visit,word,len+1,row,col+1) ||
23                 BFS(board,visit,word,len+1,row,col-1) || BFS(board,visit,word,len+1,row+1,col) ||
24                 BFS(board,visit,word,len+1,row-1,col);
25         visit[row][col] = false;
26         return tmp;
27         }else return false;
28     }
29 }

BFS 方法的代码逻辑等二刷时候再修改学习下。

时间: 2024-10-13 01:02:37

79. Word Search java solutions的相关文章

leetcode 79 Word Search ----- java

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 us

刷题79. Word Search

一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问题,就先看正确的写法,下面是回溯法的代码: class Solution { public: int m,n; //左 上 右 下 int dx[4] = {-1,0,1,0}; int dy[4] = {0,1,0,-1}; bool exist(vector<vector<char>&g

79. Word Search/212. Word Search II--图的back tracking -- tier tree 待续

79题, 给你一个二维的board, 只能往上下左右四个方向走,为你是否能找到单词. board = [ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ] Given word = "ABCCED", return true. Given word = "SEE", return true. Given word = "ABCB", return false. 分析: 算法并不难,

(Java) 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 letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be us

318. Maximum Product of Word Lengths java solutions

Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0. Example

leetCode 79.Word Search (词搜索) 解题思路和方法

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"]

【Leetcode】79. Word Search

思路: 遍历矩阵,结合dfs解即可. #include <iostream> #include <vector> using namespace std; class Solution { public: Solution() {} bool exist(vector<vector<char>>& board, string word) { //初始化都没有被访问过 this->rowCount = board.size(); if (rowC

79. 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 us

79. Word Search (Graph; DFS)

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 us