• leetcode-- 79. Word Search


    1、问题描述

    4、代码实现:

    错误示例

    class Solution {
        public boolean exist(char[][] board, String word) {
            int[][] visited = new int[board.length][board[0].length];
            for (int i = 0; i < visited.length; i++) {
                for (int j = 0; j < visited[0].length; j++) {
                    visited[i][j] = 0;
                }
            }
            return existString(board, visited, word, 0, 0, 0);
        }
    
        public boolean existString(char[][] board, int[][] visited, String word, int index, int posX, int posY) {
            if (index == word.length()) {
                return true;
            }
            if(posX < 0 || posX >= board.length
              || posY < 0 || posY >= board[0].length) {
                return false;
            }
            if (visited[posX][posY] == 1) {
                return false;
            }
            
            if (index == 0) { //index=0与其他情况还是有区别的,本来就是在寻找递归的起点,所以放在主函数里面。
                for (int i = 0; i < board.length; i++) {
                    for (int j = 0; j < board[0].length; j++) {
                        if(board[i][j] == word.charAt(index)) { //不需要这里判断,只要轮询每个点,作为起点就可以了。
                            posX = i;
                            posY = j;
                            visited[posX][posY] = 1;
                            if (true == existString(board, visited, word, index + 1, posX, posY)) {
                                return true;
                            }
                            visited[i][j] = 0;
                        }
                    }
                } 
            } else {
                if (board[posX][posY] != word.charAt(index)) {
                    return false;
                }
                if (posX + 1 >= 0 && posX + 1 < board.length && 1 != visited[posX + 1][posY]
                    && board[posX + 1][posY] == word.charAt(index + 1)) {
                    visited[posX + 1][posY] = 1; //还未检查这一点,就设置为visited,在visited检查时肯定false
                    if (true == existString(board, visited, word, index + 1, posX + 1, posY)) {
                        return true;
                    }
                    visited[posX + 1][posY] = 0;
                }
                if (posX - 1 >= 0 && posX - 1 < board.length && 1 != visited[posX - 1][posY]
                    && board[posX - 1][posY] == word.charAt(index + 1)) {
                    visited[posX - 1][posY] = 1;
                    if (true == existString(board, visited, word, index + 1, posX - 1, posY)) {
                        return true;
                    }
                    visited[posX - 1][posY] = 0;
                }
                if (posY + 1 >= 0 && posY + 1 < board[].length && 1 != visited[posX][posY + 1]
                    && board[posX][posY + 1] == word.charAt(index + 1)) {
                    visited[posX][posY + 1] = 1;
                    if (true == existString(board, visited, word, index + 1, posX, posY + 1)) {
                        return true;
                    }
                    visited[posX][posY + 1] = 0;
                }
                if (posY - 1 >= 0 && posY - 1 < board[0].length && 1 != visited[posX][posY - 1]
                    && board[posX][posY - 1] == word.charAt(index + 1)) {
                    visited[posX][posY - 1] = 1;
                    if (true == existString(board, visited, word, index + 1, posX, posY - 1)) {
                        return true;
                    }
                    visited[posX][posY - 1] = 0;
                }
            }
            return false;
        } 
    }

     修改后示例:

    思路是走到某个点,首先检查是否到base case,即字符串最后+1,然后检查该点的边界和visited,再检查是否为匹配字符,通过则标记为visited,继续向四周查找。

    class Solution {
        public boolean exist(char[][] board, String word) {
            int[][] visited = new int[board.length][board[0].length];
            for (int i = 0; i < visited.length; i++) { //初始化visited
                for (int j = 0; j < visited[0].length; j++) {
                    visited[i][j] = 0;
                }
            }
            int index = 0;        
            for (int posX = 0; posX < board.length; posX++) {
                for (int posY = 0; posY < board[0].length; posY++) {//轮询每个点,然后以此点为起点开始查找
                        if (true == existString(board, visited, word, index, posX, posY)) {//index始终为0
                            return true;
                        }
                }
            }
            return false;
        }
    
        public boolean existString(char[][] board, int[][] visited, String word, int index, int posX, int posY) {
            if (index == word.length()) { //base case
                return true;
            }
            if(posX < 0 || posX >= board.length
              || posY < 0 || posY >= board[0].length) {//检查边界
                return false;
            }
            if (visited[posX][posY] == 1) {//检查是否visited
                return false;
            }
            if (board[posX][posY] != word.charAt(index)) {//检查字符是否匹配
                return false;
            }
    
            visited[posX][posY] = 1;//以上通过则标记为visited,然后向四周轮询
            if (true == existString(board, visited, word, index + 1, posX + 1, posY)) {
                return true;
            }
    
            if (true == existString(board, visited, word, index + 1, posX - 1, posY)) {
                return true;
            }
    
            if (true == existString(board, visited, word, index + 1, posX, posY + 1)) {
                return true;
            }
    
            if (true == existString(board, visited, word, index + 1, posX, posY - 1)) {
                return true;
            }
            visited[posX][posY] = 0;//恢复现场
            return false;
        } 
    }

     进一步简化:

    class Solution {
        public boolean exist(char[][] board, String word) {
            int[][] visited = new int[board.length][board[0].length];
            for (int i = 0; i < visited.length; i++) {
                for (int j = 0; j < visited[0].length; j++) {
                    visited[i][j] = 0;
                }
            }
            int index = 0;        
            for (int posX = 0; posX < board.length; posX++) {
                for (int posY = 0; posY < board[0].length; posY++) {
                        if (existString(board, visited, word, index, posX, posY)) {
                            return true;
                        }
                }
            }
            return false;
        }
    
        public boolean existString(char[][] board, int[][] visited, String word, int index, int posX, int posY) {
            if (index == word.length()) {
                return true;
            }
            if(posX < 0 || posX >= board.length
              || posY < 0 || posY >= board[0].length) {
                return false;
            }
            if (visited[posX][posY] == 1) {
                return false;
            }
            if (board[posX][posY] != word.charAt(index)) {
                return false;
            }
    
            visited[posX][posY] = 1;
            if (existString(board, visited, word, index + 1, posX + 1, posY)
                || existString(board, visited, word, index + 1, posX - 1, posY)
                || existString(board, visited, word, index + 1, posX, posY + 1)
                || existString(board, visited, word, index + 1, posX, posY - 1)) {
                return true;
            }
            visited[posX][posY] = 0;
            return false;
        } 
    }
  • 相关阅读:
    Spring进阶—如何用Java代码实现邮件发送(一)
    如何在Apache中使用PHP处理PHP文件
    最“高大上”的Spring测试:Spring Test
    【编程直播】来约吗?
    【PaPaPa】实现缓存决策
    【PaPaPa】系统架构搭建浅析
    【PaPaPa】集成B/S主流技术的MVC5项目
    【轮子狂魔】手把手教你自造Redis Client
    【轮子狂魔】抛弃IIS,打造个性的Web Server
    【轮子狂魔】抛弃IIS,向天借个HttpListener
  • 原文地址:https://www.cnblogs.com/shihuvini/p/7441100.html
Copyright © 2020-2023  润新知