当前位置:网站首页>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 :
边栏推荐
- Ascinema with asciicast2gif for efficient command line terminal recording
- Teach you how to view version information with mongodb
- Still worried about missing measurements? Let's use Jacobo to calculate the code coverage
- Learning these 10 kinds of timed tasks, I'm a little floating
- clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]
- Installer la Bibliothèque imagemagick 7.1 et l'extension imagick de PHP
- 【Prometheus】6. Prometheus and kubernetes (incomplete)
- Cloud + community [play with Tencent cloud] essay solicitation activity winners announced
- 安装ImageMagick7.1库以及php的Imagick扩展
- 几种常见的DoS攻击
猜你喜欢

Mongodb Getting started Practical Tutoriel: Learning Summary Table des matières

Jenkins 镜像无法更新插件中心的3种解决方法

Mysql之Binlog

Intelij 中的 Database Tools可以连接但是无法显示SCHEMA, TABLES

【云原生 | Kubernetes篇】Kubernetes基础入门(三)

Linux record -4.22 MySQL 5.37 installation (supplementary)

国产最长寿的热销手机,苹果也不是对手,总算让国产手机找回面子

The catch-up of domestic chips has scared Qualcomm, the leader of mobile phone chips in the United States, and made moves to cope with the competition

我与“Apifox”的网络情缘

熬夜整理出的软件测试【高频】面试题大全(2022最新)
随机推荐
实现领域驱动设计 - 使用ABP框架 - 领域逻辑 & 应用逻辑
高速公路服务区智能一体机解决方案
One article explains Jackson configuration information in detail
Remote connection raspberry pie in VNC Viewer Mode
打破内存墙的新利器成行业“热搜”!持久内存让打工人也能玩转海量数据+高维模型
The decline of China's product managers: starting from the nostalgia for jobs
Poor remote code execution in Alien Swarm
使用阿里云RDS for SQL Server性能洞察优化数据库负载-初识性能洞察
安装ImageMagick7.1库以及php的Imagick扩展
From practical teaching to competition exercise, Tencent experts personally teach Ti-One platform operation strategy!
Industry cases of successful digital transformation
CAP:多重注意力机制,有趣的细粒度分类方案 | AAAI 2021
Why is the blackmail virus that shut down half of America's energy system terrible? Interpretation of authoritative reports
Jenkins的便捷式安装
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
安裝ImageMagick7.1庫以及php的Imagick擴展
Three solutions for Jenkins image failing to update plug-in Center
【Prometheus】5. Alertmanager alarm (incomplete)
Design of CAN bus controller based on FPGA (Part 2)
Arrays API