leetcode 212. Word Search II 单词矩阵搜索 + DFS + 字典树
来源:互联网 发布:淘宝休闲外套 编辑:程序博客网 时间:2024/05/17 05:51
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.
这道题最直觉的方法就是DFS深度优先搜索,但是会发现超时,这个我也么办法了,这个方法是最直接的方法,我认为是最好的方法。
我在网上找到了一个使用字典树的应用,说实话这个我想不到,但是还是这么做时间空间效率最好。
代码如下:
import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set;/* * 典型的DFS深度优先搜索 * 不过会超时 * */class Solution { public List<String> findWords(char[][] board, String[] words) { //这里res使用set是为了避免添加重复的元素 Set<String> res=new HashSet<String>(); if(board==null || words==null || board.length<=0 || words.length<=0) return new ArrayList<>(res); List<String> target=new ArrayList<>(); for(int i=0;i<words.length;i++) target.add(words[i]); boolean [][]visit=new boolean [board.length][board[0].length];/* StringBuilder builder=new StringBuilder(); for(int i=0;i<board.length;i++) { for(int j=0;j<board[0].length;j++) { search(board,visit,builder,target,res,i,j); } }*/ /* * 使用字典树剪枝,加速搜索 * */ Trie trie = new Trie(); for(String word : words) trie.insert(word); for(int i=0; i<board.length; i++) for(int j=0; j<board[0].length; j++) search(board, visit, trie, i, j, new StringBuilder(), res); return new ArrayList<>(res); } private void search(char[][] board,boolean[][] visited,Trie trie,int i,int j, StringBuilder sb, Set<String> res) { if(i<0 || i>board.length-1 || j<0 || j>board[0].length-1 || visited[i][j]) return; sb.append(board[i][j]); String s = sb.toString(); visited[i][j] = true; if(trie.startsWith(s)) { if(trie.search(s)) res.add(s); search(board, visited, trie, i-1, j, sb, res); search(board, visited, trie, i+1, j, sb, res); search(board, visited, trie, i, j-1, sb, res); search(board, visited, trie, i, j+1, sb, res); } sb.deleteCharAt(sb.length() - 1); visited[i][j] = false; } /* * 使用DFS直接搜索 * */ private void search(char[][] board, boolean[][] visit, StringBuilder builder, List<String> target, Set<String> res, int i, int j) { if(i>=0&&i<board.length && j>=0&&j<board[0].length && visit[i][j]==false) { builder.append(board[i][j]); visit[i][j]=true; if(target.contains(builder.toString())) res.add(builder.toString()); search(board, visit, builder, target, res, i-1, j); search(board, visit, builder, target, res, i+1, j); search(board, visit, builder, target, res, i, j+1); search(board, visit, builder, target, res, i, j-1); visit[i][j]=false; builder.deleteCharAt(builder.length()-1); }else return ; }}class TrieNode { // Initialize your data structure here. char c; boolean leaf; HashMap<Character, TrieNode> children = new HashMap<Character, TrieNode>(); public TrieNode(char c) { this.c = c; } public TrieNode(){};}class Trie { private TrieNode root; public Trie() { root = new TrieNode(); } // Inserts a word into the trie. public void insert(String word) { Map<Character, TrieNode> children = root.children; for(int i=0; i<word.length(); i++) { char c = word.charAt(i); TrieNode t; if(children.containsKey(c)) t = children.get(c); else { t = new TrieNode(c); children.put(c, t); } children = t.children; if(i==word.length()-1) t.leaf=true; } } // Returns if the word is in the trie. public boolean search(String word) { TrieNode t = searchNode(word); return t!=null && t.leaf; } public boolean startsWith(String prefix) { return searchNode(prefix) != null; } private TrieNode searchNode(String word) { Map<Character, TrieNode> children = root.children; TrieNode t = null; for(int i=0; i<word.length(); i++) { char c = word.charAt(i); if(!children.containsKey(c)) return null; t = children.get(c); children = t.children; } return t; }}
阅读全文
0 0
- leetcode 212. Word Search II 单词矩阵搜索 + DFS + 字典树
- LeetCode 212. Word Search II(单词搜索)
- LeetCode OJ 之 Word Search II (单词搜索 - 二)
- 【字典树】leetcode - Word Search II
- DFS-lintcode单词搜索(word-search)
- Leetcode 212 Word Search II 单词查找
- Leetcode Word Search搜索文中单词
- Leetcode 212 Word Search II 字典树 + 回溯
- LeetCode Word Search II DFS+Trie树
- LeetCode 212 Word Search II (Trie树+DFS)
- leetcode -- Word Search I & II -- dfs 重点
- [leetcode] 212.Word Search II
- LeetCode 212. Word Search II
- leetcode.212. Word Search II
- [leetcode] 212. Word Search II
- Leetcode 212. Word Search II
- leetcode 212. Word Search II
- Leetcode 212. Word Search II
- HDU 4115 Eliminate the Conflict(简单2-SAT)
- 配置了<mvc:default-servlet-handler />还是不能访问静态资源
- jenkins--配置
- WDM驱动针对Win8前后系统的兼容性问题(security_init_cookie+0x1b)
- 函数
- leetcode 212. Word Search II 单词矩阵搜索 + DFS + 字典树
- shell脚本下载小猪佩奇中文版全集mp3
- AI会毁灭人类??
- 网络编程之Winsock2 服务提供者接口(SPI)
- BFS 非常可乐
- Shopping Offers POJ
- 前台js(canvas)生成图片,通过后台java进行保存
- Android开发之Fragment详解
- Servlet中的service方法