212. Word Search II
再尝试用 isWord boolean 的方式创建 trienode , 做下这道题。
// first build a trie from the words from the dictinonary // traverse the board from the begininng // if the current element in the board is also in the trie // keep trying four directions, (dfs), at the same time, node is changing // in the trie as well. if we reach a word in the trie and in the board, we // add it to the list and keep dfs even aftter this isword node, because // there might be more valid words with the same prefix // if the current element in the board is not one of the children in the trie // return null; // anothe thing is to do the recovery from the backtracking. since we dont want to // visit a, and then visit b, and then visit a (no no no) // so after visiting a, we keep a's value to a temp var, and set the a's posioton as // empty, after done with this path completely, we recover a's value at the a's psoiton
Given a 2D board and a list of words from the dictionary, find all words in the board.
Each word must 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 in a word.
Example:
Input: words =["oath","pea","eat","rain"]and board = [ ['o','a','a','n'], ['e','t','a','e'], ['i','h','k','r'], ['i','f','l','v'] ] Output:["eat","oath"]
212. Word Search II // AC class Solution { class TrieNode{ TrieNode[] children = new TrieNode[26]; // 'a' - 'a' = 0 String word; } public List<String> findWords(char[][] board, String[] words) { // build trie // do dfs on the board with the help of the trie // add result List<String> result = new ArrayList<>(); TrieNode head = buildTrie(words); // traveres the board for(int i = 0; i < board.length; i++){ for(int j = 0; j < board[0].length; j++){ boolean[][] visited = new boolean[board.length][board[0].length]; dfs(head, i , j, board, result, visited); } } return result; } private void dfs(TrieNode head, int i, int j, char[][] board, List<String> result, boolean[][] visited){ // check boundary if(i >= board.length || j >= board[0].length || i < 0 || j < 0) return; // check if exists on the trie and also visited already char cur = board[i][j]; if(visited[i][j] || head.children[cur - 'a'] == null) return; // else , it exists on the trie , so arrive to this one head = head.children[cur - 'a']; if(head.word != null) { result.add(head.word); // set word as null , incase there are two or more paths with the same word head.word = null; } // since it exists,we make it as visited and do four directions traversal on it visited[i][j] = true; int[][] directions = {{0, 1},{0, -1},{1, 0},{-1, 0}}; for(int[] dir : directions){ int x = i + dir[0]; int y = j + dir[1]; dfs(head, x, y, board, result, visited); } visited[i][j] = false; } private TrieNode buildTrie(String[] words){ TrieNode head = new TrieNode(); for(String word : words){ TrieNode cur = head; for(char ch : word.toCharArray()){ int pos = ch - 'a'; if(cur.children[pos] == null){ // create a new node cur.children[pos] = new TrieNode(); // going down } cur = cur.children[pos];// here , if the pefix exists already, still need to add more chars } cur.word = word; } return head; } }
trienode 这样设定,
String word;
这样可以直接返回整个word
posted on 2018-08-09 17:28 猪猪🐷 阅读(136) 评论(0) 收藏 举报
浙公网安备 33010602011771号