由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - LeetCode上word search问题的几个例子不对
相关主题
palindrome partioning IIleetcode 的 Insert Interval 就是过不了大的
L家 Influencer 问题求讨论Word Search large case TLE
Search a 2D Matrix的两种写法,哪种更好?请教下leetcode Permutations II
[合集] G家onsite面经有人面试遇到word-ladder-ii这道题目吗?
请教一道leetcode的新题帮忙看看为撒 leetcode OJ time out "Substring with Concatenation of All Words "
Leetcode Valid Number问大牛们一个Leetcode上的题
leetcode-- scramble stringfacebook电面题目
leetcode wordsearch的时间复杂度?L二电面据,附面经
相关话题的讨论汇总
话题: column话题: indicator话题: row话题: board
进入JobHunting版参与讨论
1 (共1页)
C***U
发帖数: 2406
1
我在网上judge large的时候有几个例子出错了 错误的比率很小
我把例子拷贝到我自己机器上答案是对的
不知道为什么
这个是我的code 用recursive写的
bool existHelper(vector > &board, vector > &
indicator, int row, int column, string word, int index){
if(index == word.size()) {
return true;
}
if(row < 0 || row >= board.size() || column < 0 || column >= board[0].
size()) {
return false;
}
if(indicator[row][column]){
return false;
}
if(board[row][column] != word[index]) {
return false;
}
indicator[row][column] = true;
bool up, down, left, right;
up = existHelper(board, indicator, row - 1, column, word,index + 1);
down = existHelper(board, indicator, row + 1, column, word,index + 1);
left = existHelper(board, indicator, row, column - 1, word,index + 1);
right = existHelper(board, indicator, row, column + 1, word,index + 1);
indicator[row][column] = false;
return (up || down || left || right);
}
bool exist(vector > &board, string word) {
vector > indicator;
for(int i = 0; i < board.size(); i++) {
vector temp;
for(int j = 0; j < board[0].size(); j++) {
temp.push_back(false);
}
indicator.push_back(temp);
}
bool result = false;
for(int i = 0; i < board.size(); i++) {
for(int j = 0; j < board[0].size(); j++) {
result = result || existHelper(board, indicator, i, j, word, 0);
}
}
return result;
}
C***U
发帖数: 2406
2
我修改了一点点 这个全部通过了
但是我觉得这两个code没区别啊
bool existHelper(vector > &board, vector > &
indicator, int row, int column, string word, int index){
bool result;

if(index == word.size()) {
return true;
}
if(row < 0
|| row >= board.size()
|| column < 0
|| column >= board[0].size()
|| indicator[row][column]
|| board[row][column] != word[index])
{
return false;
}
indicator[row][column] = true;
bool up, down, left, right;
up = existHelper(board, indicator, row - 1, column, word, index + 1);
down = existHelper(board, indicator, row + 1, column, word, index +
1);
left = existHelper(board, indicator, row, column - 1, word, index +
1);
right = existHelper(board, indicator, row, column + 1, word, index +
1);

result = up || down || left || right;
if(result) {
return true;
}
else {
indicator[row][column] = false;
return false;
}
}
bool exist(vector > &board, string word) {
vector > indicator;
for(int i = 0; i < board.size(); i++) {
vector temp;
for(int j = 0; j < board[0].size(); j++) {
temp.push_back(false);
}
indicator.push_back(temp);
}
for(int i = 0; i < board.size(); i++) {
for(int j = 0; j < board[0].size(); j++) {
if(existHelper(board, indicator, i, j, word, 0)) {
return true;
}
}
}
return false;
}

【在 C***U 的大作中提到】
: 我在网上judge large的时候有几个例子出错了 错误的比率很小
: 我把例子拷贝到我自己机器上答案是对的
: 不知道为什么
: 这个是我的code 用recursive写的
: bool existHelper(vector > &board, vector > &
: indicator, int row, int column, string word, int index){
: if(index == word.size()) {
: return true;
: }
: if(row < 0 || row >= board.size() || column < 0 || column >= board[0].

1 (共1页)
进入JobHunting版参与讨论
相关主题
L二电面据,附面经请教一道leetcode的新题
写一个function判断一个数是不是2的整数次方Leetcode Valid Number
facebook的面试题leetcode-- scramble string
interleave string 的题目leetcode wordsearch的时间复杂度?
palindrome partioning IIleetcode 的 Insert Interval 就是过不了大的
L家 Influencer 问题求讨论Word Search large case TLE
Search a 2D Matrix的两种写法,哪种更好?请教下leetcode Permutations II
[合集] G家onsite面经有人面试遇到word-ladder-ii这道题目吗?
相关话题的讨论汇总
话题: column话题: indicator话题: row话题: board