摘要:兩個(gè)循環(huán)遍歷整個(gè)矩陣,出現(xiàn)則將其周圍相鄰的全部標(biāo)記為,用子函數(shù)遞歸標(biāo)記。注意里每次遞歸都要判斷邊界。寫一個(gè)的,寫熟練。
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 the island. If two 1 is adjacent, we consider them in the same island. We only consider up/down/left/right adjacent.
ExampleGiven graph:
[ [1, 1, 0, 0, 0], [0, 1, 0, 0, 1], [0, 0, 0, 1, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 1] ]
return 3.
Note兩個(gè)for循環(huán)遍歷整個(gè)矩陣,出現(xiàn)"1"則count++將其周圍相鄰的"1"全部標(biāo)記為"0",用子函數(shù)mark()遞歸標(biāo)記。
Solution DFS注意mark里每次遞歸都要判斷邊界。
public class Solution { public int numIslands(char[][] grid) { if (grid == null || grid.length == 0 || grid[0].length == 0) return 0; int count = 0; for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[0].length; j++) { if (grid[i][j] == "1") { count++; mark(grid, i, j); } } } return count; } public void mark(char[][] grid, int i, int j) { if (grid[i][j] == "1" && i >= 0 && i < grid.length && j >= 0 && j < grid[0].length) { grid[i][j] = "0"; if (i-1 >= 0) mark(grid, i-1, j); if (i+1 < grid.length) mark(grid, i+1, j); if (j-1 >= 0) mark(grid, i, j-1); if (j+1 < grid[0].length) mark(grid, i, j+1); } } }Union Find
寫一個(gè)Union Find的class,寫熟練。
public class Solution { public int numIslands(char[][] grid) { if (grid == null || grid.length == 0 || grid[0].length == 0) return 0; int m = grid.length, n = grid[0].length; UnionFind uf = new UnionFind(m, n, grid); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (grid[i][j] == "0") continue; int x = i*n+j; int y; if (i < m-1 && grid[i+1][j] == "1") { y = x+n; uf.union(x, y); } if (j < n-1 && grid[i][j+1] == "1") { y = x+1; uf.union(x, y); } } } return uf.count; } class UnionFind { public int count; public int[] parents; public UnionFind(int m, int n, char[][] grid) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (grid[i][j] == "1") count++; } } parents = new int[m*n]; for (int i = 0; i < m*n; i++) parents[i] = i; } public int find(int i) { if (i == parents[i]) return i; parents[i] = find(parents[i]); return parents[i]; } public void union(int i, int j) { int pi = find(i); int pj = find(j); if (pi == pj) return; else parents[pi] = pj; count--; } } }Number of Islands II
search another post
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/65504.html
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...
摘要:同時(shí)我們每找到一個(gè),就將其標(biāo)為,這樣就能把整個(gè)島嶼變成。我們只要記錄對矩陣遍歷時(shí)能進(jìn)入多少次搜索,就代表有多少個(gè)島嶼。 Number of Islands 最新更新的思路,以及題II的解法請?jiān)L問:https://yanjia.me/zh/2018/11/... Given a 2d grid map of 1s (land) and 0s (water), count the nu...
摘要:解題思路標(biāo)零法對這個(gè)矩陣進(jìn)行循環(huán)訪問每一個(gè)點(diǎn)當(dāng)這個(gè)點(diǎn)等于,島嶼數(shù)量,與其同時(shí)用算法訪問周圍的其他點(diǎn),進(jìn)行同樣的操作且將訪問過且等于的點(diǎn)標(biāo)記為零。版本島嶼數(shù)量搜索右邊搜索左邊搜索下邊搜索上邊 Q: Number of Islands Given a 2d grid map of 1s (land) and 0s (water), count the number of islands. ...
Problem In a given 2D binary array A, there are two islands. (An island is a 4-directionally connected group of 1s not connected to any other 1s.) Now, we may change 0s to 1s so as to connect the two...
Problem Write an algorithm to determine if a number is happy.A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squar...
閱讀 884·2019-08-30 14:05
閱讀 1773·2019-08-30 11:08
閱讀 3279·2019-08-29 15:41
閱讀 3645·2019-08-23 18:31
閱讀 1587·2019-08-23 18:29
閱讀 606·2019-08-23 14:51
閱讀 2159·2019-08-23 13:53
閱讀 2199·2019-08-23 13:02