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 }
  • 相关阅读:
    配置eclipse链接hadoop
    hdfs命令总结
    hadoop环境搭建之分布式的建立6
    win10任务视图的时间线历史记录无法删除
    Flask——server.py引入其他python文件
    《架构即未来》阅读笔记3
    msi文件无文件关联,右键打开方式找不到Windows® 安装程序
    bat文件无文件关联,双击无法启动,但可拖进dos窗口运行
    软件无法安装,每次打开安装包都是显示释放安装文件,然后就无反应了
    开始菜单右键打不开,win+x键无反应
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4101012.html
Copyright © 2011-2022 走看看