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 }
  • 相关阅读:
    gulp管理静态资源缓存
    你懂AI吗(1)
    Vue.js之render函数基础
    笑看女程序员征婚SQL,半夜巡逻民警突然对我大喊int类型占几个字节
    高吞吐量的分布式发布订阅消息系统Kafka之Producer源码分析
    Java并没有衰落.大家对它的认识才刚刚开始 Java8全新出发
    那些面试官必问的JAVA多线程和并发面试题及回答
    在阿里一位新员工是怎么一步步培养起来的
    Lambda表达式用法大比较: Scala和Java 8
    国内外程序员编程网站、博客,对学编程的你提供一点小小的帮助
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4101012.html
Copyright © 2011-2022 走看看