From 8fa5236be7264fa21d3858b6b63afbac80dd3964 Mon Sep 17 00:00:00 2001 From: YDZ Date: Sun, 28 Jul 2019 08:26:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20problem=20130?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../130. Surrounded Regions.go | 75 +++++++++++++++++++ .../130. Surrounded Regions_test.go | 49 ++++++++++++ Algorithms/0130. Surrounded Regions/README.md | 39 ++++++++++ .../547. Friend Circles.go | 75 +++++++++++-------- 4 files changed, 206 insertions(+), 32 deletions(-) create mode 100644 Algorithms/0130. Surrounded Regions/130. Surrounded Regions.go create mode 100644 Algorithms/0130. Surrounded Regions/130. Surrounded Regions_test.go create mode 100755 Algorithms/0130. Surrounded Regions/README.md diff --git a/Algorithms/0130. Surrounded Regions/130. Surrounded Regions.go b/Algorithms/0130. Surrounded Regions/130. Surrounded Regions.go new file mode 100644 index 00000000..ad24bc2d --- /dev/null +++ b/Algorithms/0130. Surrounded Regions/130. Surrounded Regions.go @@ -0,0 +1,75 @@ +package leetcode + +// 解法一 并查集 +func solve(board [][]byte) { + if len(board) == 0 { + return + } + m, n := len(board[0]), len(board) + uf := UnionFind{} + uf.init(n*m + 1) // 特意多一个特殊点用来标记 + + for i := 0; i < n; i++ { + for j := 0; j < m; j++ { + if (i == 0 || i == n-1 || j == 0 || j == m-1) && board[i][j] == 'O' { //棋盘边缘上的 'O' 点 + uf.union(i*m+j, n*m) + } else if board[i][j] == 'O' { //棋盘非边缘上的内部的 'O' 点 + if board[i-1][j] == 'O' { + uf.union(i*m+j, (i-1)*m+j) + } + if board[i+1][j] == 'O' { + uf.union(i*m+j, (i+1)*m+j) + } + if board[i][j-1] == 'O' { + uf.union(i*m+j, i*m+j-1) + } + if board[i][j+1] == 'O' { + uf.union(i*m+j, i*m+j+1) + } + + } + } + } + for i := 0; i < n; i++ { + for j := 0; j < m; j++ { + if uf.find(i*m+j) != uf.find(n*m) { + board[i][j] = 'X' + } + } + } +} + +// 解法二 DFS +func solve1(board [][]byte) { + for i := range board { + for j := range board[i] { + if i == 0 || i == len(board)-1 || j == 0 || j == len(board[i])-1 { + if board[i][j] == 'O' { + dfs130(i, j, board) + } + } + } + } + + for i := range board { + for j := range board[i] { + if board[i][j] == '*' { + board[i][j] = 'O' + } else if board[i][j] == 'O' { + board[i][j] = 'X' + } + } + } +} + +func dfs130(i, j int, board [][]byte) { + if i < 0 || i > len(board)-1 || j < 0 || j > len(board[i])-1 { + return + } + if board[i][j] == 'O' { + board[i][j] = '*' + for k := 0; k < 4; k++ { + dfs130(i+dir[k][0], j+dir[k][1], board) + } + } +} diff --git a/Algorithms/0130. Surrounded Regions/130. Surrounded Regions_test.go b/Algorithms/0130. Surrounded Regions/130. Surrounded Regions_test.go new file mode 100644 index 00000000..a363b5f0 --- /dev/null +++ b/Algorithms/0130. Surrounded Regions/130. Surrounded Regions_test.go @@ -0,0 +1,49 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question130 struct { + para130 + ans130 +} + +// para 是参数 +// one 代表第一个参数 +type para130 struct { + one [][]byte +} + +// ans 是答案 +// one 代表第一个答案 +type ans130 struct { + one [][]byte +} + +func Test_Problem130(t *testing.T) { + + qs := []question130{ + + question130{ + para130{[][]byte{}}, + ans130{[][]byte{}}, + }, + + question130{ + para130{[][]byte{[]byte{'X', 'X', 'X', 'X'}, []byte{'X', 'O', 'O', 'X'}, []byte{'X', 'X', 'O', 'X'}, []byte{'X', 'O', 'X', 'X'}}}, + ans130{[][]byte{[]byte{'X', 'X', 'X', 'X'}, []byte{'X', 'X', 'X', 'X'}, []byte{'X', 'X', 'X', 'X'}, []byte{'X', 'O', 'X', 'X'}}}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 130------------------------\n") + + for _, q := range qs { + _, p := q.ans130, q.para130 + fmt.Printf("【input】:%v ", p) + solve1(p.one) + fmt.Printf("【output】:%v \n", p) + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/0130. Surrounded Regions/README.md b/Algorithms/0130. Surrounded Regions/README.md new file mode 100755 index 00000000..f8664a19 --- /dev/null +++ b/Algorithms/0130. Surrounded Regions/README.md @@ -0,0 +1,39 @@ +# [130. Surrounded Regions](https://leetcode.com/problems/surrounded-regions/) + + + +## 题目: + +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. + +**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 + +**Explanation:** + +Surrounded regions shouldn’t be on the border, which means that any `'O'` on the border of the board are not flipped to `'X'`. Any `'O'` that is not on the border and it is not connected to an `'O'` on the border will be flipped to `'X'`. Two cells are connected if they are adjacent cells connected horizontally or vertically. + +## 题目大意 + +给定一个二维的矩阵,包含 'X' 和 'O'(字母 O)。找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O' 用 'X' 填充。被围绕的区间不会存在于边界上,换句话说,任何边界上的 'O' 都不会被填充为 'X'。 任何不在边界上,或不与边界上的 'O' 相连的 'O' 最终都会被填充为 'X'。如果两个元素在水平或垂直方向相邻,则称它们是“相连”的。 + + +## 解题思路 + + +- 给出一张二维地图,要求把地图上非边缘上的 'O' 都用 'X' 覆盖掉。 +- 这一题有多种解法。第一种解法是并查集。先将边缘上的 'O' 全部都和一个特殊的点进行 `union()` 。然后再把地图中间的 'O' 都进行 `union()`,最后把和特殊点不是同一个集合的点都标记成 'X'。第二种解法是 DFS 或者 BFS,可以先将边缘上的 'O' 先标记成另外一个字符,然后在递归遍历过程中,把剩下的 'O' 都标记成 'X'。 + diff --git a/Algorithms/0547. Friend Circles/547. Friend Circles.go b/Algorithms/0547. Friend Circles/547. Friend Circles.go index cd086be7..4e39c4db 100644 --- a/Algorithms/0547. Friend Circles/547. Friend Circles.go +++ b/Algorithms/0547. Friend Circles/547. Friend Circles.go @@ -2,60 +2,71 @@ package leetcode // 解法一 并查集 -// UninonSet defind -type UninonSet struct { - roots []int +// UnionFind defind +type UnionFind struct { + parent, rank []int + count int } -func (us UninonSet) init() { - for i := range us.roots { - us.roots[i] = i +func (uf *UnionFind) init(n int) { + uf.count = n + uf.parent = make([]int, n) + uf.rank = make([]int, n) + for i := range uf.parent { + uf.parent[i] = i } } -func (us UninonSet) findRoot(i int) int { - root := i - for root != us.roots[root] { - root = us.roots[root] +func (uf *UnionFind) find(p int) int { + root := p + for root != uf.parent[root] { + root = uf.parent[root] } - for i != us.roots[i] { - tmp := us.roots[i] - us.roots[i] = root - i = tmp + // compress path + for p != uf.parent[p] { + tmp := uf.parent[p] + uf.parent[p] = root + p = tmp } return root } -func (us UninonSet) union(p, q int) { - qroot := us.findRoot(q) - proot := us.findRoot(p) - us.roots[proot] = qroot +func (uf *UnionFind) union(p, q int) { + proot := uf.find(p) + qroot := uf.find(q) + if proot == qroot { + return + } + if uf.rank[qroot] > uf.rank[proot] { + uf.parent[proot] = qroot + } else { + uf.parent[qroot] = proot + if uf.rank[proot] == uf.rank[qroot] { + uf.rank[proot]++ + } + } + uf.count-- +} + +func (uf *UnionFind) totalCount() int { + return uf.count } func findCircleNum(M [][]int) int { - n, count := len(M), 0 + n := len(M) if n == 0 { return 0 } - us := UninonSet{} - us.roots = make([]int, n+1) - us.init() + uf := UnionFind{} + uf.init(n) for i := 0; i < n; i++ { for j := 0; j <= i; j++ { if M[i][j] == 1 { - x, y := us.findRoot(i), us.findRoot(j) - if x != y { - us.union(x, y) - } + uf.union(i, j) } } } - for i := 0; i < n; i++ { - if us.roots[i] == i { - count++ - } - } - return count + return uf.count } // 解法二 FloodFill DFS 暴力解法