zoukankan      html  css  js  c++  java
  • [LeetCode] Word Search

    Question:

    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.

    1、题型分类:

    2、思路:

      该题类似于迷宫,递归回溯。要实现从某一步开始往下找,发现找不到了,能够回到当初的那一步,那么就要将递归的地方放在搜索的过程中,要判断能否搜索到结果,就要将递归放在if语句中,搜索失败后能从这一步再次搜索。要实现不搜索找过的路,需要一个辅助数组。

    3、时间复杂度:

    4、代码:

        public boolean exist(char[][] board, String word) {
            //an array help to determine the path has searched
            if(board==null || word==null || word.length()==0) return false;
            //if(board.length==1) return new String(board[0]).indexOf(word)>=0;
            boolean [][] check=new boolean [board.length][board[0].length];
            for(int i=0;i<board.length;i++)
            {
                for(int j=0;j<board[0].length;j++)
                {
                    check[i][j]=false;
                }
            }
            for(int i=0;i<board.length;i++)
            {
                for(int j=0;j<board[0].length;j++)
                {
                    if(board[i][j]==word.charAt(0) && check[i][j]==false)
                    {
                        check[i][j]=true;
                        if(word.length()==1 || search(board, word.substring(1), i, j, check))
                            return true;
                        check[i][j]=false;
                    }
                }
            }
            return false;
        }
        public boolean search(char[][] board, String word,int y,int x,boolean[][] check)
        {
            int[][] direction={{1,0},{-1,0},{0,1},{0,-1}};
            for(int k=0;k<direction.length;k++)
            {
                int xx=x+direction[k][0];
                int yy=y+direction[k][1];
                if(xx>=0 && xx<board[0].length
                    && yy>=0 && yy<board.length
                    && board[yy][xx]==word.charAt(0) 
                    && check[yy][xx]==false)
                {
                    check[yy][xx]=true;
                    if(word.length()==1 || search(board, word.substring(1), yy, xx, check))
                        return true;
                    check[yy][xx]=false;
                }
            }
            return false;
        }

    5、优化:

    6、扩展:

  • 相关阅读:
    properties to json (通过前缀手动创建json, bean) propsutils
    Drill 常用时间函数 drill
    ubuntu20.04 修改 DNS  ip
    javascript 获取图片的尺寸 how to get image size using javascript
    javascript小数点后保留N位并可以四舍五入
    C# 递归算法求 1,1,2,3,5,8,13···
    自加入屠龙后的成长记
    Session丢值的问题
    第二个网站成长经历,http://www.chaomagou.com/ 潮妈购
    回想自己2012年1月1日到2012年6月19日的所作所为
  • 原文地址:https://www.cnblogs.com/maydow/p/4646307.html
Copyright © 2011-2022 走看看