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.

    Remember to change the character back after loop through all the possibility

     1 class Solution {
     2     public boolean exist(char[][] board, String word) {
     3         if (board == null || board.length == 0 || board[0].length == 0) return false;
     4         if (word.length() == 0) return true;
     5         
     6         for (int i = 0; i < board.length; i ++) {
     7             for (int j = 0; j < board[0].length; j ++) {
     8                 if (wordSearch(board, i, j, 0, word)) return true;
     9             }
    10         }
    11         return false;
    12     }
    13     
    14     public boolean wordSearch(char[][] board, int i, int j, int cur, String word) {
    15         if (cur == word.length()) return true;
    16         if (i < 0 || i >= board.length || j < 0 || j >= board[0].length || board[i][j] != word.charAt(cur)) return false;
    17         board[i][j] = '$'; // mark this node as visited
    18         int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    19         for (int[] dir : dirs) {
    20             if (wordSearch(board, i + dir[0], j + dir[1], cur + 1, word))
    21                 return true;
    22         }
    23         board[i][j] = word.charAt(cur);
    24         return false;
    25     }
    26 }

    非常聪明的方法1:O(1)空间的话可以不用visited数组,直接改board。改了之后,再访问的时候因为board[i][j] != word.charAt(ind)所以会return false

     1 private boolean exist(char[][] board, int i, int j, String word, int ind){
     2     if(ind == word.length()) return true;
     3     if(i > board.length-1 || i <0 || j<0 || j >board[0].length-1 || board[i][j]!=word.charAt(ind))
     4         return false;
     5     board[i][j]='*';
     6     boolean result =    exist(board, i-1, j, word, ind+1) ||
     7                         exist(board, i, j-1, word, ind+1) ||
     8                         exist(board, i, j+1, word, ind+1) ||
     9                         exist(board, i+1, j, word, ind+1);
    10     board[i][j] = word.charAt(ind);
    11     return result;

    相同的思路,非常聪明方法2:用一个bit mask,After board[y][x] ^= 256 the char became not a valid letter. After second board[y][x] ^= 256
    it became a valid letter again.

     1 public boolean exist(char[][] board, String word) {
     2     char[] w = word.toCharArray();
     3     for (int y=0; y<board.length; y++) {
     4         for (int x=0; x<board[y].length; x++) {
     5             if (exist(board, y, x, w, 0)) return true;
     6         }
     7     }
     8     return false;
     9 }
    10 
    11 private boolean exist(char[][] board, int y, int x, char[] word, int i) {
    12     if (i == word.length) return true;
    13     if (y<0 || x<0 || y == board.length || x == board[y].length) return false;
    14     if (board[y][x] != word[i]) return false;
    15     board[y][x] ^= 256;
    16     boolean exist = exist(board, y, x+1, word, i+1)
    17         || exist(board, y, x-1, word, i+1)
    18         || exist(board, y+1, x, word, i+1)
    19         || exist(board, y-1, x, word, i+1);
    20     board[y][x] ^= 256;
    21     return exist;
    22 }
  • 相关阅读:
    Android将TAB选项卡放在屏幕底部(转)
    unix进程间通信
    C优先级顺序(转)
    C/C++ 内存补齐机制
    Android Sqlite ORM 工具
    类型安全性测试
    反射手册笔记 2.程序集,对象和类型
    CLR笔记:15.委托
    反射手册笔记 4.创建对象
    反射手册笔记 1.灵活的编程方法
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/3746765.html
Copyright © 2011-2022 走看看