zoukankan      html  css  js  c++  java
  • LeetCode79. 单词搜索

    ☆☆☆☆思路:二维平面上的回溯法。

      先将问题转化为树形问题,每一个点都有四个方向可以走,若规定顺时针即为“上->右->下->左”。

      注意:关于标记某个格子是否被访问过有两个思路,一是设置visited[][]布尔数组,二是直接修改输入数据,回溯时再改回来。

         在具体面试中,需要询问面试官是否可以修改输入数据!

    代码1(设置vis[][]数组,耗时:6ms):

    class Solution {
        public boolean exist(char[][] board, String word) {
            int m = board.length;
            int n = board[0].length;
            boolean[][] visited = new boolean[m][n];
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    // 从 坐标(i,j) 开始查找
                    if (dfs(board, i, j, visited,0, word)) {
                        return true;
                    }
                }
            }
            return false;
        }
        // 从board[x][y]开始,寻找word[index...word.size())
        private boolean dfs(char[][] board, int x, int y, boolean[][] visited, int index, String word) {
            // 越界判断 以及 是否访问过 判断
            if (x < 0 || x >= board.length || y < 0 || y >= board[0].length || visited[x][y]) return false;
            if (board[x][y] != word.charAt(index)) return false;
            if (index == word.length() - 1) {
                return true;
            }
            visited[x][y] = true;
            // 顺时针方向搜索
            boolean res = dfs(board, x - 1, y, visited,index + 1, word)
                            || dfs(board, x, y + 1, visited,index + 1, word)
                            || dfs(board, x + 1, y, visited,index + 1, word)
                            || dfs(board, x, y - 1, visited,index + 1, word);
            visited[x][y] = false;
            return res;
        }
    }

    代码2(修改输入数据,耗时:6ms)

    class Solution {
        public boolean exist(char[][] board, String word) {
            int m = board.length, n = board[0].length;
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (dfs(board, i, j, 0, word)) {
                        return true;
                    }
                }
            }
            return false;
        }
        private boolean dfs(char[][] board, int x, int y, int index, String word) {
            // 记得大边界要带等号!!老是忘 T.T
            if (x < 0 || x >= board.length || y < 0 || y >= board[0].length) return false;
            if (board[x][y] != word.charAt(index)) return false;
            if (index == word.length() - 1) {
                return true;
            }
            char temp = board[x][y];
            board[x][y] = '#';
            boolean res = dfs(board, x - 1, y, index + 1, word)
                            || dfs(board, x, y + 1, index + 1, word)
                            || dfs(board, x + 1, y, index + 1, word)
                            || dfs(board, x, y - 1, index + 1, word);
            board[x][y] = temp;
            return res;
        }
    }
  • 相关阅读:
    音乐情感识别
    SoftmaxWithLoss函数和师兄给的loss有哪些区别呢
    内积层和全连接层是一样的
    caffe中的Local Response Normalization (LRN)有什么用,和激活函数区别
    caffe官网的部分翻译及NG的教程
    couldn't import dot_parser
    apt-get -f install
    Spring常用注解总结 hibernate注解
    Set Map List Iterator
    iframe 与frameset
  • 原文地址:https://www.cnblogs.com/HuangYJ/p/14204889.html
Copyright © 2011-2022 走看看