Word Search II
来源:互联网 发布:js闭包循环引用 编辑:程序博客网 时间:2024/04/30 06:34
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.
For example,
Given words = ["oath","pea","eat","rain"]
and board =
[ ['o','a','a','n'], ['e','t','a','e'], ['i','h','k','r'], ['i','f','l','v']]Return
["eat","oath"]
.Note:
You may assume that all inputs are consist of lowercase letters a-z
.
思路:这题直接暴力用backtracking会超时,hint提示用了trie。思路就是对于矩阵里面的每一个点为起点开始搜索,如果startwith trie,那么就开始搜索,search到了就加到hashset里面,最后返回。因为trie的搜索很快,所以不会超时。
public class Solution { public List<String> findWords(char[][] board, String[] words) { List<String> list = new ArrayList<String>(); if(board == null || board.length == 0 || board[0].length == 0 || words == null || words.length == 0) { return list; } Trie tries = new Trie(); for(String str: words){ tries.insert(str); } HashSet<String> hashset = new HashSet<String>(); boolean[][] visited = new boolean[board.length][board[0].length]; StringBuilder sb = new StringBuilder(); for(int i=0; i<board.length; i++){ for(int j=0; j<board[0].length; j++){ collect(board, i, j, tries, visited,sb, hashset); } } return new ArrayList<String>(hashset); } public void collect(char[][] board, int i, int j, Trie tries, boolean[][] visited, StringBuilder sb, HashSet<String> hashset){ if(i<0 || i>=board.length || j<0 || j>=board[0].length || visited[i][j] ){ return ; } sb.append(board[i][j]); visited[i][j] = true; String str = sb.toString(); if(tries.startwith(str)){ if(tries.search(str)){ hashset.add(str); } collect(board, i+1, j, tries, visited, sb, hashset); collect(board, i-1, j, tries, visited, sb, hashset); collect(board, i, j+1, tries, visited, sb, hashset); collect(board, i, j-1, tries, visited, sb, hashset); } visited[i][j] = false; sb.deleteCharAt(sb.length()-1); } public class Trie{ public class TrieNode{ TrieNode[] array; boolean isword; public TrieNode() { array = new TrieNode[26]; isword = false; } } private TrieNode root; public Trie() { root = new TrieNode(); } public void insert(String word){ if(word == null || word.length() == 0) return; TrieNode curNode = root; for(int i=0; i<word.length(); i++){ char c = word.charAt(i); if(curNode.array[c-'a'] == null) { TrieNode temp = new TrieNode(); curNode.array[c-'a'] = temp; curNode = temp; } else { curNode = curNode.array[c-'a']; } } curNode.isword = true; } public TrieNode searchNode(String word){ TrieNode curNode = root; for(int i=0; i<word.length(); i++){ char c = word.charAt(i); if(curNode.array[c-'a'] == null) { return null; } else { curNode = curNode.array[c-'a']; } } return curNode == root ? null : curNode; } public boolean search(String word){ TrieNode node = searchNode(word); return node!= null && node.isword; } public boolean startwith(String prefix){ TrieNode node = searchNode(prefix); return node!=null; } } }
0 0
- LeetCode Word Search II
- leetcode212:Word Search II
- Leetcode212-Word Search II
- [leetcode] Word Search II
- LeetCode Word Search II
- leetcode Word Search II
- Word Search II
- Word Search II
- LeetCode - Word Search II
- Word Search II
- [leetcode]Word Search II
- word search II
- Word Search II
- leetcode Word Search II
- Leetcode -- Word Search II
- [LeetCode212] Word Search II
- LeetCode Word Search II
- Leetcode @ Word Search II
- iOS 数组与数组的追加
- 深入理解java嵌套类和内部类、匿名类
- 学Android开发 这19个开发工具助你顺风顺水
- 随机森林
- listview的item全部漏出来才显示
- Word Search II
- #363 Trapping Rain Water
- 简单积分题
- Unity 5 的Assetbundle 以及unity 4的Assetbundle 食用方法
- Ubuntu 10.04 ifupdown(eth0)(永久)不能修改解决方法
- date时间
- android fragmentargs详解
- Sublime Text 3118-3120 英文菜单改进版
- Java多线程和线程池