在自定义类向量中查找
Find in Custom Class Vector
我正在练习这段代码(来自LeetCode),以便在C++中做得更好。很遗憾,我无法让"find"正常工作
此代码用于从char类型的向量(即board)中搜索单词,而不访问同一个字母两次(visitedSoFar跟踪字母visitedSo Far的x,y位置)。
Node类的向量用于存储迄今为止访问的位置
这是我写的代码片段:
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;
}
};
如果我对find进行注释,我会得到正确的输出,但这并不适用于所有测试用例。
谢谢。
编辑
在方法搜索中,更正if语句以检查(r+1)和(c+1)的大小
编辑
单词可以由顺序相邻单元格的字母构成,其中"相邻"单元格是指水平或垂直相邻的单元格。同一个字母单元格不能多次使用。
编辑
设计错误:find操作应该无法找到(表示到目前为止还没有访问过节点),然后在其中继续搜索。因此,find被更改为==visitedSoFar.end(),而不是!=visitedSoFar.end().
我认为您应该使用更简单的解决方案设计。检查每个边界点背后的想法很可能是不必要的工作,对吧?使用你的方法,你会不断地检查工作是否已经完成。此检查包括对每个搜索步骤在棋盘上进行线性搜索(每个节点都会在某个时间保存)。这意味着你很可能可以避免检查它,因为要做的工作几乎是一样的。
因此,一个快速编码的解决方案是这样的。
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
正如评论中提到的,Solution不是一个类的候选者。它可以这样写:
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
}
}
这可以从决定板中是否包含单词的界面中隐藏搜索的实现。
相关文章:
- 在 C++11 中,如何查找并返回以给定字符串开头的字符串向量中的所有项?
- 以C++递归方式查找向量中的最大值
- C++如何在向量中查找最常见的元素
- 查找两个排序向量中共有的元素
- 这是查找向量中元素位置的合法方法吗?
- 使用 C++ 查找向量的中值
- 查找向量的平均值
- 查找向量的一个向量的元素是否存在于另一个向量中
- 查找向量中数字的出现次数(以C++为单位)
- 查找向量C++中变量之间的百分比差异
- 查找向量中的最小值
- 迭代和递归地查找向量中是否存在元素
- 查找向量中的值索引位置
- 查找向量中的 n 个最大值
- 如何查找向量中元素的最大和最小索引
- 查找向量中小于阈值(复杂度)的所有数字
- 查找向量中某个元素的所有引用的索引
- 查找向量中最小+最大值元素的函数
- 在c++中查找向量中的值的差
- 如何在 C++ 中查找向量的第 m 和第 n 个元素之间的 max 元素