1. 程式人生 > 其它 >79. Word Search

79. Word Search

技術標籤:leetcode-javaleetcodedfs只能訪問一次

文章目錄

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;
            }
        }
}