zoukankan      html  css  js  c++  java
  • Word Search

    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.

    思路:

      dfs + 回溯

    我的代码:

    public class Solution {
        public boolean exist(char[][] board, String word) {
            if(board == null || board.length == 0 || board[0].length == 0 || word == null || word.length() == 0)  return false;
            int row = board.length;
            int col = board[0].length;
            boolean[][] isVisited = new boolean[row][col];
            for(int i = 0; i < row; i++)
            {
                for(int j = 0; j < col; j++)
                {
                    if(board[i][j] == word.charAt(0))
                    {
                        isVisited[i][j] = true;
                        if(helper(board, word.substring(1), i, j, isVisited, row, col))    return true;
                        isVisited[i][j] = false;
                    }
                }
            }
            return false;
        }
        public boolean helper(char[][] board, String word, int i, int j, boolean[][] isVisited, int row, int col)
        {
            if(word == null || word.length() == 0)  return true;
            char c = word.charAt(0);
            //left
            if(j > 0 && isVisited[i][j-1] == false && board[i][j-1] == c)
            {
                isVisited[i][j-1] = true;
                if(helper(board, word.substring(1), i, j-1, isVisited, row, col)) return true;
                isVisited[i][j-1] = false;
            }
            //right
            if(j < col - 1 && isVisited[i][j+1] == false && board[i][j+1] == c)
            {
                isVisited[i][j+1] = true;
                if(helper(board, word.substring(1), i, j+1, isVisited, row, col)) return true;
                isVisited[i][j+1] = false;
            }
            //up
            if(i > 0 && isVisited[i-1][j] == false && board[i-1][j] == c)
            {
                isVisited[i-1][j] = true;
                if(helper(board, word.substring(1), i-1, j, isVisited, row, col)) return true;
                isVisited[i-1][j] = false;
            }
            //down
            if(i < row-1 && isVisited[i+1][j] == false && board[i+1][j] == c)
            {
                isVisited[i+1][j] = true;
                if(helper(board, word.substring(1), i+1, j, isVisited, row, col)) return true;
                isVisited[i+1][j] = false;
            }
            return false;
        }
    }
    View Code
  • 相关阅读:
    <script>元素
    女朋友问什么是动态规划,应该怎么回答?
    从输入URL到页面展示,这中间都发生了什么?
    TypeScript之父:JS不是竞争对手,曾在惧怕开源的微软文化中艰难求生
    Flash 终将谢幕:微软将于年底停止对 Flash 的支持
    尤雨溪:TypeScript不会取代JavaScript
    JVM参数设置、分析(转发)
    -XX:PermSize -XX:MaxPermSize 永久区参数设置
    堆的分配参数
    -Xmx 和 –Xms 设置最大堆和最小堆
  • 原文地址:https://www.cnblogs.com/sunshisonghit/p/4357290.html
Copyright © 2011-2022 走看看