79. Word Search

1题目理解

Given an m x n board and a word, find if the word exists in the grid.

The word can be constructed from letters of sequentially adjacent cells, where “adjacent” cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.

输入:一个二维字符数组board和一个字符串word
规则:在board中能根据相邻元素查找,查找到完整的word。相邻元素是指横向和纵向元素相邻。每个元素只能使用一次。
输出:能找到输出true,否则输出false。
Example 1:
在这里插入图片描述
Input: board = [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word = “ABCCED”
Output: true

2 回溯

在board中查找到word中的第一个字符,然后开始递归查找。在查找过程中要记录元素是不是已经被查找过。每个元素只能查找一次。
要记录是不是查找过,可以使用visited数组记录,也可以修改board元素值,标记是否查找过。

class Solution {
    private char[][] board;
    private String word;
    private int m;
    private int n;
    private boolean[][] visited;
    public boolean exist(char[][] board, String word) {
        this.board = board;
        this.word = word;
        this.m = board.length;
        this.n = board[0].length;
        visited = new boolean[m][n];
        
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(board[i][j]==word.charAt(0)){
                    if(dfs(i,j,0)){
                        return true;
                    }
                }
            }
        }
        return false;
    }
    
    private boolean dfs(int i,int j,int wordIndex){
        if(wordIndex==word.length()){
            return true;
        }
        if(i>=m || j>=n || i<0 || j<0){
            return false;
        }
        if(visited[i][j]) return false;
        visited[i][j] = true;
        boolean r = false;
        if(board[i][j]==word.charAt(wordIndex)){
            if(dfs(i+1,j,wordIndex+1)
            || dfs(i-1,j,wordIndex+1)
            || dfs(i,j-1,wordIndex+1)
            || dfs(i,j+1,wordIndex+1))
                r =  true;
        }
        visited[i][j]=false;
        return r;
    }
}

时间复杂度: O ( m ∗ n ∗ 3 l ) O(m*n*3^l) O(mn3l) m,n 是char数组规格,l是word的长度。每个位置最多进入3次。因为访问过一次就不能再访问了。除了第一次调用的时候可以进入4次。

3 212 word search II

题目理解:跟79题类似。
输入:一个二维字符数组board和一个字符串数组words
规则:在board中能根据相邻元素查找,查找到完整的word。相邻元素是指横向和纵向元素相邻。每个元素在一个单词中只能使用一次。
输出:能找到的word。

解题:这次要查找的是数组中的每一个词。可以调用上面的方法n词,挨个查找每个词。
也可以先将words构建一棵trie树。从根节点查找每个词。因为会多次查找,为了防止加入重复的词,在找到一个词之后将节点的word置为空。

class Solution {
        private char[][] board;
        private int m;
        private int n;
        private boolean[][] visited;
        private TrieNode root=new TrieNode('/');
        private List<String> answer;
        public List<String> findWords(char[][] board, String[] words) {
            this.board = board;
            buildTrieTree(words);

            this.m = board.length;
            this.n = board[0].length;
            visited = new boolean[m][n];
            answer = new ArrayList<String>();

            for(int i=0;i<m;i++){
                for(int j=0;j<n;j++){
                    dfs(i,j,root);
                }
            }
            return answer;
        }

        private void dfs(int i,int j, TrieNode node){
            if(node.word!=null){
                answer.add(node.word);
                node.word=null;
            }
            if(i>=m || j>=n || i<0 || j<0){
                return;
            }
            if(visited[i][j]) return;
           
            char ch = board[i][j];
            if(node.children[ch-97]==null) return;
            visited[i][j] = true;
            node = node.children[ch-97];
            dfs(i+1,j,node);
            dfs(i-1,j,node);
            dfs(i,j-1,node);
            dfs(i,j+1,node);
            visited[i][j]=false;
        }



        private void buildTrieTree(String[] words){
            for(String word : words){
                insert(word);
            }
        }

        /** Inserts a word into the trie. */
        public void insert(String word) {
            TrieNode node = root;
            for(int i=0;i<word.length();i++){
                char ch = word.charAt(i);
                int index = ch - 97;
                if(node.children[index]==null){
                    node.children[index] = new TrieNode(ch);
                }
                node = node.children[index];
            }
            node.end = true;
            node.word = word;
        }


        class TrieNode{
            private char ch;
            private boolean end;
            private String word;
            private TrieNode[] children = new TrieNode[26];
            public TrieNode(char ch){
                this.ch = ch;
            }
        }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值