Leetcode 212 Word Search II 字典树 + 回溯
来源:互联网 发布:fft算法思想 编辑:程序博客网 时间:2024/05/21 12:13
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
.
直接套用之前trie的代码。
枚举每一个位置为起点进行dfs,dfs向四个方向搜索,如果当前位置的串不为树中某个词的前缀,则返回,否则看当前串是否为一个存在的单词并继续搜索。
需要注意用一个map维护出现过的单词,避免相同词重复加入结果容器。
同一个位置的字符在一个单词中只能使用一次,因此需要将搜索过的位置置为其他不为字母的字符,并在回溯时调整回来。
class node { public: int flag; node* next[26]; node(int x = 0) { flag = x; memset(next, 0, sizeof(next)); } }; class WordDictionary { public: node* root; /** Initialize your data structure here. */ WordDictionary() { root = new node(); } /** Adds a word into the data structure. */ void addWord(string word) { //cout<<word<<endl; node* p = root; for(int i = 0; i < word.size(); i++) { if(!p->next[word[i] - 'a']) p->next[word[i] - 'a'] = new node(); p = p->next[word[i] - 'a']; } p->flag = 1; } /** Returns if the word is in the trie. */ bool search(string word) { node* p = root; for(int i = 0; i < word.size(); i++) { if(!p->next[word[i] - 'a']) return false; p = p->next[word[i] - 'a']; } return p->flag == 1; } /** Returns if there is any word in the trie that starts with the given prefix. */ bool startsWith(string prefix) { node* p = root; for(int i = 0; i < prefix.size(); i++) { if(!p->next[prefix[i] - 'a']) return false; p = p->next[prefix[i] - 'a']; } return true; } }; class Solution {public: WordDictionary tree; unordered_map<string, bool> mp; void dfs(vector<string> &res, vector<vector<char>>& board, int i,int j, string now) { if(board[i][j] == ' ') return ; now += board[i][j]; if(!tree.startsWith(now)) return ; if(tree.search(now) && !mp[now]) { mp[now] = true; res.push_back(now); } char temp = board[i][j]; board[i][j] = ' '; if(i < board.size() - 1) dfs(res, board, i + 1, j, now); if(i > 0) dfs(res, board, i - 1, j, now); if(j < board[0].size() - 1) dfs(res, board, i, j + 1, now); if(j > 0) dfs(res, board, i, j - 1, now); board[i][j] = temp; } vector<string> findWords(vector<vector<char>>& board, vector<string>& words) { vector<string> res; int m = board.size(); if(m == 0) return res; int n = board[0].size(); for(int i = 0; i < words.size(); i++) tree.addWord(words[i]); for(int i = 0; i < m; i++) { for(int j = 0; j < n; j++) { string temp; dfs(res, board, i, j, temp); } } return res; }};
1 0
- Leetcode 212 Word Search II 字典树 + 回溯
- 【字典树】leetcode - Word Search II
- leetcode---Word Search---回溯
- leetcode 212: Word Search II
- Leetcode #212 Word Search II
- leetcode 212: Word Search II
- 212 Word Search II [Leetcode]
- Leetcode (212) Word Search II
- leetcode(212). Word Search II
- leetcode Add and Search Word - Data structure design回溯算法+Trie字典树
- LeetCode Word Search II
- [leetcode] Word Search II
- LeetCode Word Search II
- leetcode Word Search II
- LeetCode - Word Search II
- [leetcode]Word Search II
- leetcode Word Search II
- Leetcode -- Word Search II
- 没有考虑异常输入的你,总有一天会急死。
- webbrowser里的js与程序交互问题
- gitlab 命令
- 游戏界面组建Morn
- 一步两步带你实现Android沉浸式设计
- Leetcode 212 Word Search II 字典树 + 回溯
- docker 命令小结
- 23种设计模式分析(2):创建型模式
- Scala基础应用(5)- 函数式编程
- Windows下启动Tomcat 访问localhost:8080遇到“java.lang.IllegalStateException: No output folder”
- 3月20号
- flume+kafka+storm整合02---问题
- PE文件详解(五)
- 求任意一点是否在三角形内部