對于 leetcode 200 Number of Islands,這個解決方案作業正常。但是,對于較低的解決方案,由于超過了時間限制,提交失敗。根據我的理解,這兩個解決方案應該同時運行。你能幫忙嗎?
class Solution {
// BFS time O(mn) | space O(min(m,n))
public int numIslands(char[][] grid) {
int islandsCount = 0;
for (int i = 0; i < grid.length; i ) {
for (int j = 0; j < grid[0].length; j ) {
if (grid[i][j] == '1') {
islandsCount ;
bfs(grid, i, j);
}
}
}
return islandsCount;
}
public static void bfs(char[][] grid, int row, int col) {
int rc = grid.length;
int cc = grid[0].length;
Deque<Integer> queue = new ArrayDeque<Integer>();
queue.offer(row*cc col);
while (!queue.isEmpty()) {
Integer spotIdx = queue.poll();
int i = spotIdx/cc, j = spotIdx
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/380762.html
