摘要:不得不說(shuō),對(duì)于這道題而言,是一種很木訥的模板。主函數(shù)按矩陣大小建立一個(gè)類(lèi)進(jìn)行后續(xù)操作一個(gè)結(jié)點(diǎn)用來(lái)連接邊角不可能被圍繞的一個(gè)函數(shù)對(duì)矩陣元素進(jìn)行結(jié)點(diǎn)線性化。同理,,,在主函數(shù)中初始化后調(diào)用。
Surrounded Regions Problem
Given a 2D board containing "X" and "O" (the letter O), capture all regions surrounded by "X".
A region is captured by flipping all "O"s into "X"s in that surrounded region.
For example,
X X X X X O O X X X O X X O X X
After running your function, the board should be:
X X X X X X X X X X X X X O X XNote
不得不說(shuō),對(duì)于這道題而言,Union Find是一種很木訥的模板。
一個(gè)UnionFind類(lèi)進(jìn)行后續(xù)操作;
一個(gè)dummy結(jié)點(diǎn)用來(lái)連接邊角不可能被X圍繞的O;
一個(gè)node()函數(shù)對(duì)矩陣元素進(jìn)行結(jié)點(diǎn)線性化。
遍歷所有結(jié)點(diǎn),矩陣邊角的O與dummy相連,矩陣內(nèi)部的O與相鄰的O相連;
遍歷所有結(jié)點(diǎn),與dummy相連的結(jié)點(diǎn)設(shè)置為O,其它所有結(jié)點(diǎn)設(shè)置為X。
全局變量parents;
初始化函數(shù)UnionFind(int size);
查找parents函數(shù)find(int node);
歸并函數(shù)union(int node1, int node2);
測(cè)試兩結(jié)點(diǎn)是否相連函數(shù)isConnected(int node1, int node2);
find(int node): 用while循環(huán)向上查找node的parent,注意先向上迭代parents[node],再迭代node,否則會(huì)超時(shí);
union(int node1, int node2): 找到兩個(gè)結(jié)點(diǎn)的parents,r1和r2,令parents[r1] = r2;
isConnected(int n1, int n2): 查看兩個(gè)結(jié)點(diǎn)的parents是否相等。
Solutionpublic class Solution { int row, col; public void solve(char[][] board) { if (board == null || board.length == 0) return; row = board.length; col = board[0].length; UnionFind uf = new UnionFind(row*col+1); int dummy = row*col; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (board[i][j] == "O") { if (i == 0 || i == row-1 || j == 0 || j == col-1) uf.union(node(i, j), dummy); else { if (i > 0 && board[i-1][j] == "O") uf.union(node(i, j), node(i-1, j)); if (i > 0 && board[i+1][j] == "O") uf.union(node(i, j), node(i+1, j)); if (j > 0 && board[i][j-1] == "O") uf.union(node(i, j), node(i, j-1)); if (j > 0 && board[i][j+1] == "O") uf.union(node(i, j), node(i, j+1)); } } } } for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (uf.isConnected(node(i, j), dummy)) board[i][j] = "O"; else board[i][j] = "X"; } } } public int node(int i, int j) { return i*col+j; } } class UnionFind { int[] parents; public UnionFind(int n) { parents = new int[n]; for (int i = 0; i < n; i++) parents[i] = i; } public void union(int n1, int n2) { int r1 = find(n1); int r2 = find(n2); if (r1 != r2) parents[r1] = r2; } public int find(int node) { if (parents[node] == node) return node; parents[node] = find(parents[node]); return parents[node]; } public boolean isConnected(int n1, int n2) { return find(n1) == find(n2); } }Graph Valid Tree Problem
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree.
For example:
Given n = 5 and edges = [[0, 1], [0, 2], [0, 3], [1, 4]], return true.
Given n = 5 and edges = [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]], return false.
Note同理,find,union,在主函數(shù)中初始化后調(diào)用。
Solutionpublic class Solution { int[] parents; public boolean validTree(int n, int[][] edges) { if (edges.length != n-1) return false; parents = new int[n]; for (int i = 0; i < n; i++) parents[i] = i; for (int i = 0; i < n-1; i++) { if (find(edges[i][0]) == find(edges[i][1])) return false; union(edges[i][0], edges[i][1]); } return true; } public int find(int n) { if (parents[n] == n) return n; parents[n] = find(parents[n]); return parents[n]; } public void union(int n1, int n2) { int r1 = find(n1); int r2 = find(n2); if (r1 != r2) parents[r1] = r2; } }Number of Islands Problem
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 00000
Answer: 1
Example 2:
11000 11000 00100 00011
Answer: 3
Note Solutionpublic class Solution { 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--; } public boolean isConnected(int i, int j) { return find(i) == find(j); } } 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; } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/66059.html
摘要:并查集包括查詢和聯(lián)合,主要使用不相交集合查詢主要是用來(lái)決定不同的成員是否在一個(gè)子集合之內(nèi)聯(lián)合主要是用來(lái)把多個(gè)子集合成一個(gè)集合的實(shí)際運(yùn)用計(jì)算機(jī)網(wǎng)絡(luò)檢查集群是否聯(lián)通電路板檢查不同的電路元件是否聯(lián)通初始化聯(lián)通與檢測(cè)與是否聯(lián)通返回聯(lián)通的數(shù) 并查集(Union-Find)包括查詢(Find)和聯(lián)合(Union),主要使用不相交集合(Disjoint-Sets)查詢(Find)主要是用來(lái)決定不同的...
摘要:兩個(gè)循環(huán)遍歷整個(gè)矩陣,出現(xiàn)則將其周?chē)噜彽娜繕?biāo)記為,用子函數(shù)遞歸標(biāo)記。注意里每次遞歸都要判斷邊界。寫(xiě)一個(gè)的,寫(xiě)熟練。 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...
摘要:題目鏈接這道題和是一個(gè)思路,一個(gè)初始化為,每次有新的就兩個(gè)節(jié)點(diǎn),如果兩個(gè)節(jié)點(diǎn)原來(lái)不在一個(gè)連通圖里面就減少并且連起來(lái),如果原來(lái)就在一個(gè)圖里面就不管。用一個(gè)索引來(lái)做,優(yōu)化就是加權(quán)了,每次把大的樹(shù)的當(dāng)做,小的樹(shù)的作為。 323. Number of Connected Components in an Undirected Graph 題目鏈接:https://leetcode.com/pr...
摘要:題目要求提供一個(gè)二維數(shù)組表示一張地圖,其中代表陸地,代表海洋。這里使用一個(gè)新的二維數(shù)組來(lái)表示對(duì)應(yīng)地圖上的元素屬于哪個(gè)并查集。在合并的時(shí)候先進(jìn)行判斷,如果二者為已經(jīng)相連的陸地,則無(wú)需合并,否則將新的二維數(shù)組上的元素指向所在的并查集。 題目要求 Given a 2d grid map of 1s (land) and 0s (water), count the number of isla...
單眼三維成像是依據(jù)單獨(dú)監(jiān)控?cái)z像頭健身運(yùn)動(dòng)仿真模擬雙目視覺(jué)獲得物件和空間里的三維視覺(jué)信息內(nèi)容,下面本文關(guān)鍵為大家介紹了對(duì)于如何依據(jù)python完成單眼三維成像的資料,原文中依據(jù)案例編碼推薦的十分詳盡,必須的小伙伴可以借鑒一下 一、單眼三維成像簡(jiǎn)述 客觀現(xiàn)實(shí)的物件是三維立體的,而我用監(jiān)控?cái)z像頭獲得的圖象是二維動(dòng)畫(huà)的,但我們可以依據(jù)二維圖像認(rèn)知總體目標(biāo)三維信息內(nèi)容。三維重建技術(shù)要以相對(duì)應(yīng)的形式解...
閱讀 3656·2023-04-26 02:10
閱讀 1477·2021-11-22 15:25
閱讀 1741·2021-09-22 10:02
閱讀 985·2021-09-06 15:02
閱讀 3542·2019-08-30 15:55
閱讀 667·2019-08-30 13:58
閱讀 2841·2019-08-30 12:53
閱讀 3132·2019-08-29 12:38