题目:
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.
Example:
Input: board = [ ['o','a','a','n'], ['e','t','a','e'], ['i','h','k','r'], ['i','f','l','v'] ] words =["oath","pea","eat","rain"]
Output:["eat","oath"]
Note:
- All inputs are consist of lowercase letters
a-z
. - The values of
words
are distinct.
代码:
方法一——超时普通解法:
class Solution {
public:
bool helper(string s, int i, int j, vector<vector<char>>&board) {
if (s == "")return true;
if (i >= board.size() || i < 0 || j < 0 || j >= board[i].size())return false;
if (s[0] != board[i][j])return false;
board[i][j] = '-';
bool res= helper(s.substr(1), i + 1, j, board) || helper(s.substr(1), i, j + 1, board) || helper(s.substr(1), i - 1, j,board) || helper(s.substr(1), i, j - 1, board);
board[i][j] = s[0];
return res;
}
bool isfind(vector<vector<char>>& board, string s) {
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board[i].size(); j++) {
if (helper(s, i, j, board))return true;
}
}
return false;
}
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
vector<string> res;
for (int i = 0; i < words.size(); i++) {
if (isfind(board, words[i])) {
res.push_back(words[i]);
}
}
return res;
}
};
方法二——使用了字典树:
class Trie {
public:
char c;
vector<Trie*> v;
bool is_word = false;
Trie() {
for (int i = 0; i < 26; i++) {
v.push_back(NULL);
}
c = '.';
}
void insert(string s) {
int len = s.length();
Trie* root = this;
for (int i = 0; i < len; i++) {
if (root->v[s[i] - 'a'] == NULL) {
root->v[s[i] - 'a'] = new Trie();
root->v[s[i] - 'a']->c = s[i];
}
root = root->v[s[i] - 'a'];
}
root->is_word = true;
}
bool search(string s) {
int len = s.length();
Trie* root = this;
for (int i = 0; i < len; i++) {
if (root->v[s[i] - 'a'] == NULL)return false;
root = root->v[s[i] - 'a'];
}
if (!root->is_word)return false;
return true;
}
bool prefix(string s) {
int len = s.length();
Trie* root = this;
for (int i = 0; i < len; i++) {
if (root->v[s[i] - 'a'] == NULL)return false;
root = root->v[s[i] - 'a'];
}
return true;
}
};
class Solution {
public:
vector<string> res = {};
void helper(vector<vector<char>>& board, int i, int j, Trie* t, string s) {
if (!t)return;
if (board[i][j] != t->c)return;
s.push_back(t->c);
if (t->is_word&&(find(res.begin(),res.end(),s)==res.end())) {
res.push_back(s);
}
board[i][j] = '.';
if (i > 0 && board[i - 1][j] != '.') {
helper(board, i - 1, j, t->v[board[i - 1][j] - 'a'], s);
}
if (i < board.size() - 1 && board[i + 1][j] != '.') {
helper(board, i + 1, j, t->v[board[i + 1][j] - 'a'], s);
}
if (j > 0 && board[i][j - 1] != '.') {
helper(board, i, j - 1, t->v[board[i][j - 1] - 'a'], s);
}
if (j < board[i].size() - 1 && board[i][j + 1] != '.') {
helper(board, i, j + 1, t->v[board[i][j + 1] - 'a'], s);
}
board[i][j] = t->c;
}
vector<string> findWord(vector<vector<char>>& board, Trie* tr, vector<string>& words) {
int len = words.size();
for (int i = 0; i < len; i++) {
tr->insert(words[i]);
}
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board[i].size(); j++) {
helper(board, i, j, tr->v[board[i][j] - 'a'], "");
}
}
return res;
}
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
Trie* tr = new Trie();
return findWord(board, tr, words);
}
};
想法:
踩过的坑——
(1),没用字典树
(2), 遇到isword的词过早return,没想过它下面也可能有isword的情况
(3),没考虑vector中的重复情况