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"]
.
1.我的解答 纯dfs 超时
//
// main.cpp
// 212. Word Search II
//
// Created by zjl on 16/10/11.
// Copyright © 2016年 zjl. All rights reserved.
//
#include <iostream>
#include <vector>
using namespace std;
int orinal[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
bool find_res(vector<vector<char>>board, string word, vector<vector<bool>>&visit, int level, int row, int col){
if(word[level] != board[row][col]) return false;
if(level == word.size()-1)
return true;
visit[row][col] = true;
int rows = board.size(), cols = board[0].size();
for(int i = 0; i < 4; i++){
int r = row + orinal[i][0];
int c = col + orinal[i][1];
if(r >= 0 && r < rows && c >= 0 && c < cols && visit[r][c] == false){
bool b = find_res(board, word, visit, level+1, r,c);
if(b){
visit[row][col] = false;
return true;
}
}
}
visit[row][col] = false;
return false;
}
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
vector<string>res;
if(board.size() == 0 || board[0].size() == 0 || words.size() == 0) return res;
int row = board.size();
int col = board[0].size();
int len = words.size();
vector<vector<bool>>visit(row, vector<bool>(col, false));
for(int k= 0; k < len; k++){
bool b = false;
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
if(find_res(board, words[k], visit, 0, i, j)){
b = true;
break;
}
}
if(b) break;
}
if(b){
res.push_back(words[k]);
}
}
//去重
sort(res.begin(), res.end());
res.erase(unique(res.begin(), res.end()), res.end());
return res;
}
int main(int argc, const char * argv[]) {
// insert code here...
vector<vector<char>> board= {{'o','a','a','n'},{'e','t','a','e'},{'i','h','k','r'},{'i','f','l','v'}};
vector<string> words = {"oath","pea","eat","rain"};
//vector<vector<char>> board= {{'a'}};
//vector<string> words = {"a"};
vector<string> res = findWords(board, words);
for(auto a: res)
cout<<a<<endl;
return 0;
}
觉得自己是对的,但是超时了,跟别人对比代码觉得也没问题啊。。。 那么再后续解决吧
class Solution {
private:
int orinal[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
public:
struct TrieNode{
TrieNode* next[26];
string word;//如果这条分支上到达这个点已经是一个string了,就把这个string记录下来
};
TrieNode* buildTrie(vector<string>words){
TrieNode* p = new TrieNode();
for(int i = 0; i < words.size(); i++){
string s = words[i];
TrieNode* root = p;
for(int j = 0; j < s.size(); j++){
if(root->next[s[j]-'a'] == NULL) root->next[s[j]-'a'] = new TrieNode();
root = root->next[s[j] - 'a'];
}
root->word = s;//在字符串的最后一个字符上存入字符串
}
return p;
}
void find_res(vector<vector<char>>board, TrieNode* p, int row, int col,vector<string>&res){
char c = board[row][col];
//判断当前的字符是否就是p的下一个节点
if(board[row][col]=='#' || p->next[c-'a'] == NULL) return;
p = p->next[c-'a'];
//visit[row][col] = true;
board[row][col] = '#';
if(!p->word.empty()){
res.push_back(p->word);
p->word.clear(); //防止重复加入
}
int rows = board.size(), cols = board[0].size();
for(int i = 0; i < 4; i++){
int r = row + orinal[i][0];
int c = col + orinal[i][1];
if(r >= 0 && r < rows && c >= 0 && c < cols){
find_res(board, p, r,c, res);
}
}
//visit[row][col] = false;
board[row][col] = c;
}
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
vector<string>res;
if(board.size() == 0 || board[0].size() == 0 || words.size() == 0) return res;
int row = board.size();
int col = board[0].size();
int len = words.size();
TrieNode* root = buildTrie(words);
//vector<vector<bool>>visit(row, vector<bool>(col, false));
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
find_res(board, root, i, j, res);
}
}
return res;
}
};