当前位置:网站首页>2021-04-18: given a two-dimensional array matrix, the value in it is either 1 or 0,
2021-04-18: given a two-dimensional array matrix, the value in it is either 1 or 0,
2022-06-24 15:52:00 【Fuda scaffold constructor's daily question】
2021-04-18: Given a two-dimensional array matrix, The value in it is not 1 Namely 0, On 、 Next 、 Left 、 Right adjacent 1 Think it's an island , return matrix The number of Nakajima .
Fuda answer 2021-04-18:
Union checking set .
The code to use golang To write . The code is as follows :
package main
import "fmt"
func main() {
arr := [][]byte{
{49, 49, 49, 49, 48},
{49, 49, 48, 49, 48},
{49, 49, 48, 49, 48},
{49, 49, 48, 48, 48},
{48, 48, 48, 48, 48}}
ret := numIslands(arr)
fmt.Println(ret)
}
func numIslands(board [][]byte) int {
row := len(board)
col := len(board[0])
uf := NewUnionFind(board)
for j := 1; j < col; j++ {
if board[0][j-1] == '1' && board[0][j] == '1' {
uf.union(0, j-1, 0, j)
}
}
for i := 1; i < row; i++ {
if board[i-1][0] == '1' && board[i][0] == '1' {
uf.union(i-1, 0, i, 0)
}
}
for i := 1; i < row; i++ {
for j := 1; j < col; j++ {
if board[i][j] == '1' {
if board[i][j-1] == '1' {
uf.union(i, j-1, i, j)
}
if board[i-1][j] == '1' {
uf.union(i-1, j, i, j)
}
}
}
}
return uf.getSets()
}
type UnionFind2 struct {
parent []int
size []int
help []int
col int
sets int
}
func NewUnionFind(board [][]byte) *UnionFind2 {
ret := &UnionFind2{}
ret.col = len(board[0])
ret.sets = 0
row := len(board)
length := row * ret.col
ret.parent = make([]int, length)
ret.size = make([]int, length)
ret.help = make([]int, length)
for r := 0; r < row; r++ {
for c := 0; c < ret.col; c++ {
if board[r][c] == '1' {
i := ret.index(r, c)
ret.parent[i] = i
ret.size[i] = 1
ret.sets++
}
}
}
return ret
}
// (r,c) -> i
func (this *UnionFind2) index(r int, c int) int {
return r*this.col + c
}
// Original location -> Subscript
func (this *UnionFind2) find(i int) int {
hi := 0
for i != this.parent[i] {
this.help[hi] = i
hi++
i = this.parent[i]
}
for hi--; hi >= 0; hi-- {
this.parent[this.help[hi]] = i
}
return i
}
func (this *UnionFind2) union(r1 int, c1 int, r2 int, c2 int) {
i1 := this.index(r1, c1)
i2 := this.index(r2, c2)
f1 := this.find(i1)
f2 := this.find(i2)
if f1 != f2 {
if this.size[f1] >= this.size[f2] {
this.size[f1] += this.size[f2]
this.parent[f2] = f1
} else {
this.size[f2] += this.size[f1]
this.parent[f1] = f2
}
this.sets--
}
}
func (this *UnionFind2) getSets() int {
return this.sets
}The results are as follows :
边栏推荐
- QTreeWidget作为单例模式以dll返回的两个问题
- 60 divine vs Code plug-ins!!
- Solution of intelligent all in one machine in expressway service area
- Poor remote code execution in Alien Swarm
- [log service CLS] Tencent cloud log4j/logback log collection best practices
- I just came back from the Ali software test. I worked for Alibaba P7 in 3+1, with an annual salary of 28*15
- Junit5中的参数化测试(Parameterized Tests)指南
- 我与“Apifox”的网络情缘
- Istio practical tips: Customize Max_ body_ size
- The equipment is connected to the easycvr platform through the national standard gb28181. How to solve the problem of disconnection?
猜你喜欢

推荐几款超级实用的数据分析利器

我与“Apifox”的网络情缘

Most common usage of vim editor

Solution to the problem that FreeRTOS does not execute new tasks

nifi从入门到实战(保姆级教程)——环境篇

Database tools in intelij can connect but cannot display schema, tables

60 个神级 VS Code 插件!!

Remote connection raspberry pie in VNC Viewer Mode
![clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]](/img/f0/42f394dbc989d381387c7b953d2a39.jpg)
clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]

【附下载】汉化版Awvs安装与简单使用
随机推荐
Attacked! Cloud development monitoring alarm practice
Poor remote code execution in Alien Swarm
How to optimize performance
Reference to junit5 test framework in gradle
Tencent cloud native intelligent data Lake Conference will be held, revealing the panoramic matrix of Tencent cloud data Lake products for the first time
CAP:多重注意力机制,有趣的细粒度分类方案 | AAAI 2021
日志记录真没你想的那么简单
Most common usage of vim editor
Teach you how to view version information with mongodb
clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]
Flink Kubernetes Application部署
Linux记录-4.22 MySQL5.37安装(补充)
HMM to CRF understanding and learning notes
Task priority motion planning of floating base
Jenkins的便捷式安装
Very exciting! 12000 words summarized the theory of network technology, reviewing the old and learning the new
推荐几款超级实用的数据分析利器
Istio FAQ: region awareness does not take effect
MySQL replication series 6- tables related to replication information