国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

【LC總結(jié)】Union Find系列(Num of Islands I&II/Graph V

bergwhite / 2392人閱讀

摘要:不得不說,對于這道題而言,是一種很木訥的模板。主函數(shù)按矩陣大小建立一個類進行后續(xù)操作一個結(jié)點用來連接邊角不可能被圍繞的一個函數(shù)對矩陣元素進行結(jié)點線性化。同理,,,在主函數(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 X
Note

不得不說,對于這道題而言,Union Find是一種很木訥的模板。

主函數(shù):按矩陣大小建立:

一個UnionFind類進行后續(xù)操作;

一個dummy結(jié)點用來連接邊角不可能被X圍繞的O;

一個node()函數(shù)對矩陣元素進行結(jié)點線性化。

操作:

遍歷所有結(jié)點,矩陣邊角的O與dummy相連,矩陣內(nèi)部的O與相鄰的O相連;

遍歷所有結(jié)點,與dummy相連的結(jié)點設(shè)置為O,其它所有結(jié)點設(shè)置為X。

UnionFind函數(shù):建立:

全局變量parents;

初始化函數(shù)UnionFind(int size);

查找parents函數(shù)find(int node);

歸并函數(shù)union(int node1, int node2);

測試兩結(jié)點是否相連函數(shù)isConnected(int node1, int node2);

操作:

find(int node): 用while循環(huán)向上查找node的parent,注意先向上迭代parents[node],再迭代node,否則會超時;

union(int node1, int node2): 找到兩個結(jié)點的parents,r1和r2,令parents[r1] = r2;

isConnected(int n1, int n2): 查看兩個結(jié)點的parents是否相等。

Solution
public 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)用。

Solution
public 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 Solution
public 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)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/66059.html

相關(guān)文章

  • Leetcode之Union-Find(并查集)

    摘要:并查集包括查詢和聯(lián)合,主要使用不相交集合查詢主要是用來決定不同的成員是否在一個子集合之內(nèi)聯(lián)合主要是用來把多個子集合成一個集合的實際運用計算機網(wǎng)絡(luò)檢查集群是否聯(lián)通電路板檢查不同的電路元件是否聯(lián)通初始化聯(lián)通與檢測與是否聯(lián)通返回聯(lián)通的數(shù) 并查集(Union-Find)包括查詢(Find)和聯(lián)合(Union),主要使用不相交集合(Disjoint-Sets)查詢(Find)主要是用來決定不同的...

    roland_reed 評論0 收藏0
  • [LeetCode/LintCode] Number of Islands [DFS]

    摘要:兩個循環(huán)遍歷整個矩陣,出現(xiàn)則將其周圍相鄰的全部標記為,用子函數(shù)遞歸標記。注意里每次遞歸都要判斷邊界。寫一個的,寫熟練。 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...

    Fourierr 評論0 收藏0
  • 323. Number of Connected Components in an Undirect

    摘要:題目鏈接這道題和是一個思路,一個初始化為,每次有新的就兩個節(jié)點,如果兩個節(jié)點原來不在一個連通圖里面就減少并且連起來,如果原來就在一個圖里面就不管。用一個索引來做,優(yōu)化就是加權(quán)了,每次把大的樹的當做,小的樹的作為。 323. Number of Connected Components in an Undirected Graph 題目鏈接:https://leetcode.com/pr...

    zsy888 評論0 收藏0
  • leetcode200. Number of Islands

    摘要:題目要求提供一個二維數(shù)組表示一張地圖,其中代表陸地,代表海洋。這里使用一個新的二維數(shù)組來表示對應(yīng)地圖上的元素屬于哪個并查集。在合并的時候先進行判斷,如果二者為已經(jīng)相連的陸地,則無需合并,否則將新的二維數(shù)組上的元素指向所在的并查集。 題目要求 Given a 2d grid map of 1s (land) and 0s (water), count the number of isla...

    Zoom 評論0 收藏0
  • 怎樣依據(jù)python完成單眼三維成像詳細說明

      單眼三維成像是依據(jù)單獨監(jiān)控攝像頭健身運動仿真模擬雙目視覺獲得物件和空間里的三維視覺信息內(nèi)容,下面本文關(guān)鍵為大家介紹了對于如何依據(jù)python完成單眼三維成像的資料,原文中依據(jù)案例編碼推薦的十分詳盡,必須的小伙伴可以借鑒一下  一、單眼三維成像簡述  客觀現(xiàn)實的物件是三維立體的,而我用監(jiān)控攝像頭獲得的圖象是二維動畫的,但我們可以依據(jù)二維圖像認知總體目標三維信息內(nèi)容。三維重建技術(shù)要以相對應(yīng)的形式解...

    89542767 評論0 收藏0

發(fā)表評論

0條評論

bergwhite

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<