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

[leedcode 212] Word Search II

时间:2015-08-06 12:35:05      阅读:118      评论: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"].

class Solution {
public:
    vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
      /*  刚看到题目想到之前有一道Word Search,所以马上想到暴力搜索每一个单词,但是很不幸跟预想的一样超时了,看了一下hint,提到了字典树。所以先用所给的单词来构建字典树,然后在dfs搜索字典树中的单词,这样就避免了大量的重复比较。另外如果搜索的路径已经不是字典树是的前缀了就可以直接剪枝返回了。下面是AC代码。因为要避免重复,我先用了一个set存结果,然后再转存到result中。*/
    }
};

 

[leedcode 212] Word Search II

标签:

原文地址:http://www.cnblogs.com/qiaomu/p/4707309.html

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