由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Leetcode 的 Surrounded Regions 好难过大OJ (in JAVA)
相关主题
请教一下,leetcode surrounded regions这题为什么我的代码会超时Leetcode Surrounded Regions Large Case Runtime Error
问leetcode上surrounded regions,新的test case出runtime errorSurrounded Regions 题目没读懂
leetcode上的大oj和小oj有什么本质差别吗?LeetCode那个surrounded regions解法不对吧?
Surrounded Regionsleetcode Surrounded Regions
大家帮我看看这段code 哪儿错了现在DFS过不了surround region了?
Surrounded Regions 问题问到题目
Surrounded Regions, dfs solution. 过不了online test来问一下leetcode Surrounded Regions
弱问OJ的Surrounded Regions那题Surrounded Regions 做DFS的空间复杂度是多少
相关话题的讨论汇总
话题: board话题: int话题: arraydeque话题: integer话题: static
进入JobHunting版参与讨论
1 (共1页)
B*****7
发帖数: 137
1
自己写了一个code,和大多数网上的code一样,基本思路就是bfs。但要过大oj还真不
容易。我refactor了半天也只能有大概一半的概率过。过的时候时间都是800 ms 以下
(~750 ms),只有一次到过800 ms. 所以强烈怀疑这道题的时间上限是800 ms. LOL
我附了code,用java写的。大牛们帮忙看看该怎么改进?先谢谢啦。
public class Solution {
public void solve(char[][] board) {
// Start typing your Java solution below
// DO NOT write main() function
int m = board.length;
if( m == 0 ) return;
int n = board[0].length;
// special cases
if( m < 3 || n < 3 ) return;
// process edges
//-- top and bottom
for(int j = 0; j < n; j++ ) {
if( board[0][j] == 'O' ) Helper.bfs( board, 0, j );
if( board[m-1][j] == 'O' ) Helper.bfs( board, m - 1, j );
}
//-- left and right
for(int i = 1; i < m - 1; i++ ) {
if( board[i][0] == 'O' ) Helper.bfs( board, i, 0 );
if( board[i][n-1] == 'O' ) Helper.bfs( board, i, n - 1 );
}
// change back not surrounded blocks
for( int i = 0; i < m ; i++ ) {
for( int j = 0; j < n; j++ ) {
if( board[i][j] == 'Y' ) board[i][j] = 'O';
else if( board[i][j] == 'O' ) board[i][j] = 'X';
}
}
}
static class Helper {
// for BFS
private static ArrayDeque iqueue = new ArrayDeque(
);
private static ArrayDeque jqueue = new ArrayDeque(
);
public static void bfs( char[][] board, int i, int j ) {
int m = board.length;
int n = board[0].length;
board[i][j] = 'Y';
iqueue.add( i );
jqueue.add( j );
while( !iqueue.isEmpty() ) {
i = iqueue.poll();
j = jqueue.poll();
// neighbors: [i+/-1, j], [i, j+/-1]
// [i+1,j]
if( i < m - 1 && board[i+1][j] == 'O' ) {
board[i+1][j] = 'Y';
iqueue.add( i+1 );
jqueue.add( j );
}
// [i-1, j]
if( i > 0 && board[i-1][j] == 'O' ) {
board[i-1][j] = 'Y';
iqueue.add( i-1 );
jqueue.add( j );
}
// [i, j+1]
if( j < n - 1 && board[i][j+1] == 'O' ) {
board[i][j+1] = 'Y';
iqueue.add( i );
jqueue.add( j+1 );
}
// [i, j-1]
if( j > 0 && board[i][j-1] == 'O' ) {
board[i][j-1] = 'Y';
iqueue.add( i );
jqueue.add( j-1 );
}
}
}
}
}
n********s
发帖数: 12
2
这个会把一个点重复加进去的,会造成很多重复,需要检查点是否已经加到queue里面去
p****e
发帖数: 3548
3
C++的都是 50ms的。。。
a*****a
发帖数: 46
4
C++的差距那么大?我没想明白为什么这个题Java和C++有那么大差距,也没有用
arraylist啊,难道java的函数调用比C++耗时多?哪位大牛给指点一下吧~~

【在 p****e 的大作中提到】
: C++的都是 50ms的。。。
1 (共1页)
进入JobHunting版参与讨论
相关主题
Surrounded Regions 做DFS的空间复杂度是多少大家帮我看看这段code 哪儿错了
Cisco onsite进了第二轮,拿offer的机会有多大呢。。。Surrounded Regions 问题
会计硕士需要办身份找什么样的公司?Surrounded Regions, dfs solution. 过不了online test
急问一般regional的firm会不会做国内工作那段的reference check?弱问OJ的Surrounded Regions那题
请教一下,leetcode surrounded regions这题为什么我的代码会超时Leetcode Surrounded Regions Large Case Runtime Error
问leetcode上surrounded regions,新的test case出runtime errorSurrounded Regions 题目没读懂
leetcode上的大oj和小oj有什么本质差别吗?LeetCode那个surrounded regions解法不对吧?
Surrounded Regionsleetcode Surrounded Regions
相关话题的讨论汇总
话题: board话题: int话题: arraydeque话题: integer话题: static