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.


    题解:实现一个DFS函数 private boolean canFind(char[][] board,int i,int j,String left,boolean[][] visited) ,从(i,j)出开始搜索串left,visited数组记录某个位置是否包含在当前搜索的路径中,因为每个字符只能被使用一次。

    代码如下:

     1 public class Solution {
     2         private boolean canFind(char[][] board,int i,int j,String left,boolean[][] visited){
     3         //if we have found one path
     4         if(left.equals(""))
     5             return true;
     6 
     7         //look around point(i,j) see if we can find next char
     8         char next = left.charAt(0);
     9         if(i-1>=0 && !visited[i-1][j] && board[i-1][j]== next ){
    10             visited[i-1][j] = true; 
    11             if(canFind(board, i-1, j, left.substring(1),visited))
    12                 return true;
    13             visited[i-1][j]= false; 
    14         }
    15 
    16         if(j-1>=0 && !visited[i][j-1] && board[i][j-1]== next ){
    17             visited[i][j-1] = true; 
    18             if(canFind(board, i, j-1, left.substring(1),visited))
    19                 return true;
    20             visited[i][j-1]= false; 
    21         }
    22 
    23         if(i+1 < board.length && !visited[i+1][j] && board[i+1][j]== next ){
    24             visited[i+1][j] = true; 
    25             if(canFind(board, i+1, j, left.substring(1),visited))
    26                 return true;
    27             visited[i+1][j]= false; 
    28         }
    29 
    30 
    31         if(j+1 < board[0].length && !visited[i][j+1] && board[i][j+1]== next ){
    32             visited[i][j+1] = true; 
    33             if(canFind(board, i, j+1, left.substring(1),visited))
    34                 return true;
    35             visited[i][j+1]= false; 
    36         }
    37         
    38         return false;
    39     }
    40     public boolean exist(char[][] board, String word) {
    41         if(word == null || word.length() == 0)
    42             return true;
    43         if(board.length == 0)
    44             return false;
    45         
    46         int m = board.length;
    47         int n = board[0].length;
    48         char now = word.charAt(0);
    49         boolean[][] visited = new boolean[m][n];
    50         
    51         //search board for our first char in word
    52         for(int i = 0;i < m;i++){
    53             for(int j = 0;j < n;j ++){
    54                 if(board[i][j]== now ){
    55                     visited[i][j]= true; 
    56                     if(canFind(board, i, j, word.substring(1),visited))
    57                         return true;
    58                     visited[i][j]= false; 
    59                 }
    60             }
    61         }
    62         
    63         return false;
    64     }
    65 }
  • 相关阅读:
    监控系统
    RocketMQ入门介绍
    Linux的虚拟内存详解(MMU、页表结构) 转
    快速排序
    如何选择分布式事务解决方案? 转
    java 基本数据类型相关思考
    互联网项目中mysql应该选什么事务隔离级别 转
    线上服务的FGC问题排查,看这篇就够了! 转
    什么是Base64? 转
    业界难题-“跨库分页”的四种方案 转
  • 原文地址:https://www.cnblogs.com/sunshineatnoon/p/3865013.html
Copyright © 2011-2022 走看看