zoukankan      html  css  js  c++  java
  • Leetcode-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.

    Analysis:

    Typical DFS problem. Need to consider the step of backtracking.

    Solution:

     1 public class Solution {
     2     public boolean exist(char[][] board, String word) {
     3         int xLen = board.length;
     4         if (xLen==0) return false;
     5         int yLen = board[0].length;
     6         if (yLen==0) return false;
     7 
     8         boolean[][] visited = new boolean[xLen][yLen];
     9         for (int i=0;i<xLen;i++)
    10             for (int j=0;j<yLen;j++)
    11                 visited[i][j] = false;
    12  
    13         char target = word.charAt(0);
    14         for (int i=0;i<xLen;i++)
    15             for (int j=0;j<yLen;j++)
    16                 if (board[i][j]==target){
    17                     //NOTE:We should consider about the case that the word.length is 1.
    18                     if (word.length()==1) return true;
    19                     visited[i][j]=true;
    20                     boolean find = searchRecur(board,visited,i,j,1,word);
    21                     visited[i][j]=false;
    22                     if (find) return true;
    23                 }
    24         return false;
    25     }
    26 
    27     public boolean searchRecur(char[][] board, boolean[][] visited, int x, int y, int cur, String word){
    28         int xLen = board.length;
    29         int yLen = board[0].length;
    30         char target = word.charAt(cur);
    31         if (cur==word.length()-1){
    32             if (x+1<xLen && board[x+1][y]==target && !visited[x+1][y]) return true;
    33             if (y+1<yLen && board[x][y+1]==target && !visited[x][y+1]) return true;
    34             if (x-1>=0 && board[x-1][y]==target && !visited[x-1][y]) return true;
    35             if (y-1>=0 && board[x][y-1]==target && !visited[x][y-1]) return true;
    36             return false;
    37         }
    38 
    39 
    40         if (x+1<xLen && board[x+1][y]==target && !visited[x+1][y]){
    41             visited[x+1][y] = true;
    42             boolean next = searchRecur(board,visited,x+1,y,cur+1,word);
    43             visited[x+1][y] = false;
    44             if (next) return true;
    45         }
    46 
    47         if (y+1<yLen && board[x][y+1]==target && !visited[x][y+1]){
    48             visited[x][y+1] = true;
    49             boolean next = searchRecur(board,visited,x,y+1,cur+1,word);
    50             visited[x][y+1] = false;
    51             if (next) return true;
    52         }
    53 
    54         if (x-1>=0 && board[x-1][y]==target && !visited[x-1][y]){
    55             visited[x-1][y] = true;
    56             boolean next = searchRecur(board,visited,x-1,y,cur+1,word);
    57             visited[x-1][y] = false;
    58             if (next) return true;
    59         }
    60 
    61         if (y-1>=0 && board[x][y-1]==target && !visited[x][y-1]){
    62             visited[x][y-1] = true;
    63             boolean next = searchRecur(board,visited,x,y-1,cur+1,word);
    64             visited[x][y-1] = false;
    65             if (next) return true;
    66         }
    67   
    68         return false;
    69     }
    70 }
  • 相关阅读:
    sql练手
    简单工厂模式和抽象工厂模式的区别:面向接口编程
    UML中聚合和组合的关系(笔记)
    如何修改SQL Server 2005服务器名称 (装载)
    .NET 图片处理剪裁
    sql server 中的汉字转化为拼音
    sql server while, case,if..else ... try catch ..对象
    String 和 StringBuilder 的相同点和不同点
    sql 折分字符串并修改数据库表中数据
    SQL Server 与 Excel,Access 数据表的导入导出(注:参照博园.NET大观)
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4101012.html
Copyright © 2011-2022 走看看