GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — LeetCode/Union_Find/305.Number-of-Islands-II at master · wisdompeak/LeetCode

Expand file tree

/

305.Number-of-Islands-II

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

305.Number-of-Islands-II

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
305.Number-of-Islands-II.cpp
305.Number-of-Islands-II.cpp
 
 
Readme.md
Readme.md
 
 

Readme.md

305.Number-of-Islands-II

设置所有点的初始Father为-1,表示海洋。

然后依次遍历每一块新陆地,最开始标记它的Root为自身,然后count++。依次考察这个新陆地相邻的四块:如果相邻的是陆地,并且新陆地和老陆地的Root不同,那么说明这是需要合并的两个集合,于是count--,并且将新陆地与旧陆地进行Union。最终实时输出count。

有一个corner case是,positions里面可能会包含重复的同一块陆地。所以每遍历一块新陆地的时候,得先看看是否已经访问过了,已经访问过了就不要再重新标记Root,否则会出错。

Leetcode Link

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.