码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode: Word Search II

时间:2015-12-18 06:56:28      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

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"].

如果跟Word Search做法一样,还按照DFS回溯的方法,逐个检查每个word是否在board里,显然效率是比较低的。我们可以利用Trie数据结构,也就是前缀树。然后dfs时,如果当前形成的单词不在Trie里,就没必要继续dfs下去了。如果当前字符串在trie里,就说明board可以形成这个word。

 

需要小心的几点,我犯了错误:

1. 有可能board里面有多份某一个word,dfs会把它们都找出来,但是result set只需要添加一次就好了

有人是这样做的:Set<String> res = new HashSet<String>();

return的时候: return new ArrayList<String>(res);

2. 第21行 string concatenation actually creates a new string, and have the reference cur refer to the new string. It does not change the content, so no changes will remain after the recursion. We do not need to do string shrinking like this cur = cur.substring(0, cur.length()-1) at the end of the recursion;

3. 26行 return 千万不能要,否则会出如下的错

Input:

["ab","aa"]
["aba","baa","bab","aaab","aaa","aaaa","aaba"]

Expected answer
["aaa","aba","aaba","baa","aaab"]
Your answer
["aaa","aba","baa","aaba"]

aaa被添加之后,return了,导致另一个正确答案aaab不被添加,这跟以前的情况不同,以前都是答案等长,这里不一定
 1     public List<String> findWords(char[][] board, String[] words) {
 2         List<String> res = new ArrayList<String>();
 3         if (board==null || board.length==0 || words==null || words.length==0) return res;
 4         Trie trie = new Trie();
 5         for (String str : words) {
 6             trie.insert(str);
 7         }
 8         
 9         boolean[][] visited = new boolean[board.length][board[0].length];
10         for (int i=0; i<board.length; i++) {
11             for (int j=0; j<board[0].length; j++) {
12                 dfs(res, "", i, j, board, visited, trie);
13             }
14         }
15         return res;
16     }
17     
18     public void dfs(List<String> res, String cur, int i, int j, char[][] board, boolean[][] visited, Trie trie) {
19         if (i<0 || j<0 || i>=board.length || j>=board[0].length) return;
20         if (visited[i][j]) return;
21         cur += board[i][j];
22         if (!trie.startsWith(cur)) return;
23         if (trie.search(cur)) {
24             if (!res.contains(cur))
25                 res.add(cur);
26             //return;
27         }
28         //cur = cur + board[i][j];
29         visited[i][j] = true;
30         dfs(res, cur, i-1, j, board, visited, trie);
31         dfs(res, cur, i+1, j, board, visited, trie);
32         dfs(res, cur, i, j-1, board, visited, trie);
33         dfs(res, cur, i, j+1, board, visited, trie);
34         //cur = cur.substring(0, cur.length()-1);
35         visited[i][j] = false;
36     }

 

Leetcode: Word Search II

标签:

原文地址:http://www.cnblogs.com/EdwardLiu/p/5055920.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!