[英]Find in Custom Class Vector
我正在練習這個代碼(來自LeetCode)在C ++中更好。 不幸的是,我無法“ 找到 ”正常工作。
此代碼用於從char(即board )類型的向量向量中搜索單詞而不訪問相同的字母兩次( visitedSoFar保持跟蹤字母visitedSoFar的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;
}
};
如果我評論查找我得到正確的輸出,但這不適用於所有測試用例。
謝謝。
編輯
在方法搜索中,更正了if語句以檢查(r + 1)和(c + 1)的大小。
編輯
該單詞可以由順序相鄰的單元的字母構成,其中“相鄰”單元是水平或垂直相鄰的單元。 相同的字母單元格不得多次使用。
編輯
設計錯誤:查找操作應該無法找到(表示該節點到目前為止尚未訪問過),然后繼續搜索。 因此改變了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
正如評論中所提到的,解決方案不是一個班級的候選人。 它可以寫成這樣:
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
}
}
這可以隱藏從界面中搜索的實現,以確定板中是否包含單詞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.