Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of the maximum area of the island by golang's leetcode browsing technique

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the golang leetcode skills how to solve the maximum area of the island, the article is very detailed, has a certain reference value, interested friends must read it!

Given a non-empty two-dimensional array grid that contains some zeros and ones.

An island is a combination of adjacent 1s (representing land), where "adjacent" requires that the two 1s must be adjacent horizontally or vertically. You can assume that all four edges of the grid are surrounded by 0 (for water).

Find the largest island area in a given two-dimensional array. (if there are no islands, the return area is 0.)

Example 1:

[[0,0,1,0,0,0,0,1,0,0,0,0,0]

[0,0,0,0,0,0,0,1,1,1,0,0,0]

[0,1,1,0,1,0,0,0,0,0,0,0,0]

[0,1,0,0,1,1,0,0,1,0,1,0,0]

[0,1,0,0,1,1,0,0,1,1,1,0,0]

[0,0,0,0,0,0,0,0,0,0,1,0,0]

[0,0,0,0,0,0,0,1,1,1,0,0,0]

[0,0,0,0,0,0,0,1,1,0,0,0,0]]

6 should be returned for the given matrix above. Note that the answer should not be 11, because the island can only contain 1 in four horizontal or vertical directions.

Example 2:

[[0,0,0,0,0,0,0,0]]

For the given matrix above, 0 is returned.

Note: the length and width of a given matrix grid are no more than 50.

Ideas for solving the problem:

1. This problem is similar to the one in moments, but the goal is not the same.

2. The problem of moments is to solve the number of connected domains, and here is to solve the area of connected domains.

3, solution: depth-first traversal

4. The easiest way to traverse depth first is recursion.

What is more complicated than the problem of moments here is that the problem of moments is symmetrical, so it is an one-dimensional problem, and this problem is a two-dimensional problem.

6. Tip: whether to access is generally saved with map. At that time, it needs to be determined for golang to access map, so the simple method is saved with slice.

Code implementation:

Func maxAreaOfIsland (grid [] [] int) int {visited:=make ([] [] int,len (grid)) for iPlux

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report