我正在练习这个代码(来自LeetCode)在C ++中更好。不幸的是,我无法让“找到”正常工作
此代码用于从char类型的向量向量中搜索单词(即 board ),而不会访问相同的字母两次( visitedSoFar 保留跟踪被访问过的字母的x,y位置。
类节点的向量用于存储到目前为止访问的位置
这是我写的代码片段:
class Node{
private:
int x;
int y;
public:
Node(int a, int b):x(a),y(b){};
bool operator==(Node newNode){
if(this->x == newNode.x && this->y == newNode.y)
return true;
else
return false;
}
};
class Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
vector <Node> visitedSoFar;
for(int r =0; r< board.size(); r++){
for(int c=0; c<board[r].size(); c++){
if(board[r][c] == word.at(0)){
if(search(board, word, visitedSoFar, board[r].size(), r, c))
return true;
}
}
}
return false;
}
private:
bool search(vector<vector<char>>& board, string word, vector<Node>& visitedSoFar, int size, int r, int c){
Node newNode(r,c);
visitedSoFar.push_back(newNode);
if(word.size() == 1)
return true;
Node toSearch1(r-1,c);
if(r-1 >= 0 && find(visitedSoFar.begin(), visitedSoFar.end(), toSearch1) == visitedSoFar.end()){
if(board[r-1][c] == word.at(1))
if(search(board, word.substr(1), visitedSoFar, size, r-1, c))
return true;
}
Node toSearch2(r+1,c);
if(r+1 < size && find(visitedSoFar.begin(), visitedSoFar.end(), toSearch2) == visitedSoFar.end()){
if(board[r+1][c] == word.at(1))
if(search(board, word.substr(1), visitedSoFar, size, r+1, c))
return true;
}
Node toSearch3(r,c-1);
if(c-1 >= 0 && find(visitedSoFar.begin(), visitedSoFar.end(), toSearch3) == visitedSoFar.end()){
if(board[r][c-1] == word.at(1))
if(search(board, word.substr(1), visitedSoFar, size, r, c-1))
return true;
}
Node toSearch4(r,c+1);
if(c+1 < size && find(visitedSoFar.begin(), visitedSoFar.end(), toSearch4) == visitedSoFar.end()){
if(board[r][c+1] == word.at(1))
if(search(board, word.substr(1), visitedSoFar, size, r, c+1))
return true;
}
visitedSoFar.pop_back();
return false;
}
};
如果我对查找发表评论,我会得到正确的输出,但这不适用于所有测试用例。
谢谢。
修改
在方法搜索中,更正了if语句以检查(r + 1)和(c + 1)的大小。
修改
该单词可以由顺序相邻的单元的字母构成,其中“相邻”单元是水平或垂直相邻的单元。相同的字母单元格不得多次使用。
修改
设计错误:查找操作应该无法找到(表示该节点到目前为止尚未访问过),然后继续搜索。因此改变了find = = visitedSoFar.end()而不是!= visitedSoFar.end()。
答案 0 :(得分:0)
我认为您应该使用更简单的解决方案设计。 检查每个电路板背后的想法很可能是不必要的工作,对吧?使用您的方法,您不断检查工作是否已经完成。对于每个搜索步骤,此检查包括通过电路板进行线性搜索(每个节点将在某个时间保存)。这意味着你几乎可以避免检查它,因为要完成的工作几乎是一样的。
因此,快速编码的解决方案将是这样的。
bool row_contains_word(vector<char> const& row, string word)
{
if(word.size() > row.size())
throw std::invalid_argument("Word is longer then the row!!");
// linear search for the word in board
for(int i = 0; i < row.size() - word.size(); ++i) // start point
{
// check realtive to the start point if its the word there
for(int j = 0; j < word.size(); ++j)
{
if(row[i+j] != word[j])
break; // we can continue to check the next position
// last position we check here, and match means its the word
else if(j == (word.size() - 1) && row[i+j] == word[j])
return true;
}
}
return false;
使用这个功能(我不认为它是一个非常好的方法来实现它,但只是为了得到一个例子)你可以简单地循环:
for(int r = 0; r < board.size(); ++r)
{
if(row_contains_word(board[r], word))
return true;
}
// and same with colums as well
正如评论中所提到的,解决方案不是班级的候选人。 它可以这样写:
namespace Solution
{
bool search(vector<vector<char>> const& board, string word); // implementaion follows
namespace // anonymous namespace, not accessible from the outside world, but only this compilation unit(.cpp file)
{
bool row_contains_word(vector<char> const& row, string word);
bool col_contains_word(/*vector<char> const& row,*/ string word); // this needs more work, since the col is a little different
}
}
这可以隐藏从界面中搜索的实现,以确定板中是否包含一个单词。