当前位置:网站首页>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 :
边栏推荐
- Solution of intelligent all in one machine in expressway service area
- Flink Kubernetes Application部署
- 一文详解JackSon配置信息
- 打破内存墙的新利器成行业“热搜”!持久内存让打工人也能玩转海量数据+高维模型
- The penetration of 5g users of operators is far slower than that of 4G. The popularity of 5g still depends on China Radio and television
- 推荐几款超级实用的数据分析利器
- Network engineers must know the network essence knowledge!
- Paper: Google TPU
- 使用阿里云RDS for SQL Server性能洞察优化数据库负载-初识性能洞察
- MySQL development specification
猜你喜欢
Software test [high frequency] interview questions sorted out by staying up late (latest in 2022)
Solution of intelligent all in one machine in expressway service area
The penetration of 5g users of operators is far slower than that of 4G. The popularity of 5g still depends on China Radio and television
Jenkins 镜像无法更新插件中心的3种解决方法
Most common usage of vim editor
Using alicloud RDS for SQL Server Performance insight to optimize database load - first understanding of performance insight
Recommend several super practical data analysis tools
How to expand disk space on AWS host
Implement Domain Driven Design - use ABP framework - domain logic & application logic
[my advanced OpenGL learning journey] learning notes of OpenGL coordinate system
随机推荐
10 hands-free idea plug-ins. These codes do not need to be written (the second bullet)
手机同花顺股票开户安全吗!
【附下载】汉化版Awvs安装与简单使用
Istio practical skill: hide the automatically added server header
Industry cases of successful digital transformation
【云原生 | Kubernetes篇】Kubernetes基础入门(三)
如何实现容器内的SqlServer的数据库迁移
【Prometheus】5. Alertmanager alarm (incomplete)
MongoDB入門實戰教程:學習總結目錄
一文理解OpenStack网络
[log service CLS] a taste of Tencent cloud log service CLS
In 2021, big companies often ask IOS interview questions -- runloop
How to expand disk space on AWS host
【我的OpenGL学习进阶之旅】OpenGL的坐标系的学习笔记
Monitoring and warning | is the website attacked?
几种常见的DoS攻击
Nifi from introduction to practice (nanny level tutorial) - environment
CAP:多重注意力机制,有趣的细粒度分类方案 | AAAI 2021
[log service CLS] Tencent cloud log4j/logback log collection best practices
great! The novel website project is completely open source