摘要:同時我們每找到一個,就將其標為,這樣就能把整個島嶼變成。我們只要記錄對矩陣遍歷時能進入多少次搜索,就代表有多少個島嶼。
Number of Islands 最新更新的思路,以及題II的解法請訪問:https://yanjia.me/zh/2018/11/...
標零法 復雜度Given a 2d grid map of "1"s (land) and "0"s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
11110 11010 11000 00000Answer: 1
Example 2:
11000 11000 00100 00011Answer: 3
時間 O(NM) 空間 O(max(N,M)) 遞歸棧空間
思路我們遍歷矩陣的每一個點,對每個點都嘗試進行一次深度優先搜索,如果搜索到1,就繼續向它的四周搜索。同時我們每找到一個1,就將其標為0,這樣就能把整個島嶼變成0。我們只要記錄對矩陣遍歷時能進入多少次搜索,就代表有多少個島嶼。
代碼public class Solution { public int numIslands(char[][] grid) { int res = 0; if(grid.length==0) return res; for(int i = 0; i < grid.length; i++){ for(int j = 0; j < grid[0].length; j++){ if(grid[i][j]=="1"){ searchIsland(grid, i, j); res++; } } } return res; } private void searchIsland(char[][] grid, int i, int j){ grid[i][j]="0"; // 搜索該點連通的上下左右 if(i>0 && grid[i-1][j]=="1") searchIsland(grid, i-1, j); if(j>0 && grid[i][j-1]=="1") searchIsland(grid, i, j-1); if(i2018/2
class Solution: def numIslands(self, grid): """ :type grid: List[List[str]] :rtype: int """ count = 0 for row in range(0, len(grid)): for col in range(0, len(grid[0])): if grid[row][col] == "1": self.exploreIsland(grid, row, col) count += 1 return count def exploreIsland(self, grid, row, col): grid[row][col] = "0" if (row > 0 and grid[row - 1][col] == "1"): self.exploreIsland(grid, row - 1, col) if (col > 0 and grid[row][col - 1] == "1"): self.exploreIsland(grid, row, col - 1) if (row < len(grid) - 1 and grid[row + 1][col] == "1"): self.exploreIsland(grid, row + 1, col) if (col < len(grid[0]) - 1 and grid[row][col + 1] == "1"): self.exploreIsland(grid, row, col + 1)2018/10
func explore(grid *[][]byte, row int, col int) { (*grid)[row][col] = "0" if row > 0 && (*grid)[row-1][col] == "1" { explore(grid, row-1, col) } if row < len((*grid))-1 && (*grid)[row+1][col] == "1" { explore(grid, row+1, col) } if col > 0 && (*grid)[row][col-1] == "1" { explore(grid, row, col-1) } if col < len((*grid)[0])-1 && (*grid)[row][col+1] == "1" { explore(grid, row, col+1) } } func numIslands(grid [][]byte) int { count := 0 for i, row := range grid { for j, val := range row { if val == "1" { explore(&grid, i, j) count++ } } } return count }后續 Follow UpQ:如何找湖的數量呢?湖的定義是某個0,其上下左右都是同一個島嶼的陸地。
A:我們可以先用Number of island的方法,把每個島標記成不同的ID,然后過一遍整個地圖的每個點,如果是0的話,就DFS看這塊連通水域是否被同一塊島嶼包圍,如果出現了不同數字的陸地,則不是湖。public class NumberOfLakes { public static void main(String[] args){ NumberOfLakes nof = new NumberOfLakes(); int[][] world = {{1,1,1,0,1},{1,0,0,1,0},{1,1,1,1,1,},{0,1,1,0,1},{0,1,1,1,1}}; System.out.println(nof.numberOfLakes(world)); } public int numberOfLakes(int[][] world){ int islandId = 2; for(int i = 0; i < world.length; i++){ for(int j = 0; j < world[0].length; j++){ if(world[i][j] == 1){ findIsland(world, i, j, islandId); islandId++; } } } int lake = 0; for(int i = 0; i < world.length; i++){ for(int j = 0; j < world[0].length; j++){ if(world[i][j] == 0){ // 找到當前水域的鄰接陸地編號 int currId = 0; if(i > 0) currId = (world[i - 1][j] != 0 ? world[i - 1][j] : currId); if(j > 0) currId = (world[i][j - 1] != 0 ? world[i][j - 1] : currId); if(i < world.length - 1) currId = (world[i + 1][j] != 0 ? world[i + 1][j] : currId); if(j < world[0].length - 1) currId = (world[i][j + 1] != 0 ? world[i][j + 1] : currId); // 如果該點是湖,加1 if(findLake(world, i, j, currId)){ lake++; } } } } return lake; } private boolean findLake(int[][] world, int i, int j, int id){ // 將當前水域標記成周邊島嶼的數字 world[i][j] = id; // 找上下左右是否是同一塊島嶼的陸地,如果是水域則繼續DFS,如果當前水域是邊界也說明不是湖 boolean up = i != 0 && (world[i - 1][j] == id || (world[i - 1][j] == 0 && findLake(world, i - 1, j, id))); boolean down = i != world.length - 1 && (world[i + 1][j] == id || (world[i + 1][j] == 0 && findLake(world, i + 1, j, id))); boolean left = j != 0 && (world[i][j - 1] == id || (world[i][j - 1] == 0 && findLake(world, i, j - 1, id))); boolean right = j != world[0].length - 1 && (world[i][j + 1] == id || (world[i][j + 1] == 0 && findLake(world, i, j + 1, id))); return up && down && right && left; } private void findIsland(int[][] world, int i, int j, int id){ world[i][j] = id; if(i > 0 && world[i - 1][j] == 1) findIsland(world, i - 1, j, id); if(j > 0 && world[i][j - 1] == 1) findIsland(world, i, j - 1, id); if(i < world.length - 1 && world[i + 1][j] == 1) findIsland(world, i + 1, j, id); if(j < world[0].length - 1 && world[i][j + 1] == 1) findIsland(world, i, j + 1, id); } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/64546.html
摘要:解題思路標零法對這個矩陣進行循環訪問每一個點當這個點等于,島嶼數量,與其同時用算法訪問周圍的其他點,進行同樣的操作且將訪問過且等于的點標記為零。版本島嶼數量搜索右邊搜索左邊搜索下邊搜索上邊 Q: Number of Islands Given a 2d grid map of 1s (land) and 0s (water), count the number of islands. ...
摘要:兩個循環遍歷整個矩陣,出現則將其周圍相鄰的全部標記為,用子函數遞歸標記。注意里每次遞歸都要判斷邊界。寫一個的,寫熟練。 Number of Islands Problem Given a boolean/char 2D matrix, find the number of islands. 0 is represented as the sea, 1 is represented as...
Problem Given a non-empty 2D array grid of 0s and 1s, an island is a group of 1s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are s...
摘要:題目要求提供一個二維數組表示一張地圖,其中代表陸地,代表海洋。這里使用一個新的二維數組來表示對應地圖上的元素屬于哪個并查集。在合并的時候先進行判斷,如果二者為已經相連的陸地,則無需合并,否則將新的二維數組上的元素指向所在的并查集。 題目要求 Given a 2d grid map of 1s (land) and 0s (water), count the number of isla...
摘要:在線網站地址我的微信公眾號完整題目列表從年月日起,每天更新一題,順序從易到難,目前已更新個題。這是項目地址歡迎一起交流學習。 這篇文章記錄我練習的 LeetCode 題目,語言 JavaScript。 在線網站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公眾號: showImg(htt...
閱讀 1421·2021-10-08 10:05
閱讀 3070·2021-09-26 10:10
閱讀 887·2019-08-30 15:55
閱讀 510·2019-08-26 11:51
閱讀 447·2019-08-23 18:10
閱讀 3855·2019-08-23 15:39
閱讀 663·2019-08-23 14:50
閱讀 772·2019-08-23 14:46