剑指offer12
问题相关代码
class Solution {
public:
bool flag;
vector<int> mx = {1,-1,0,0};
vector<int> my = {0,0,1,-1};
int nx,ny;
bool exist(vector<vector<char>>& board, string word) {
flag = false;
for(int i=0; i<board.size(); i++){
for(int j=0; j<board[0].size(); j++){
if(!flag&&board[i][j]==word[0]){
board[i][j] = '\0';
backReverse(board, word, 1, i, j);
board[i][j] = word[0];
}
}
}
return flag;
}
void backReverse(vector<vector<char>>& board, string& word, int ind, int x, int y){
if(ind==word.size()){
flag = true;
}
for(int i=0; i<4; i++){
nx = mx[i]+x;
ny = my[i]+y;
if(!flag&&nx>=0&&nx<board.size()&&ny>=0&&ny<board[0].size()&&board[nx][ny]!=1){
if(board[nx][ny]==word[ind]){
board[nx][ny] = '\0';
backReverse(board, word, ind+1, nx, ny);
board[nx][ny] = word[ind];
}
}
}
}
};
运行结果及报错内容
Line 1033: Char 34: runtime error: addition of unsigned offset to 0x602000000090 overflowed to 0x60200000008f (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:34
我的解答思路和尝试过的方法
我想要达到的结果