694. Number of Distinct Islands

复制代码
package LeetCode_694

import java.util.*
import kotlin.collections.HashSet

/**
 * 694. Number of Distinct Islands
 * (Prime)
 * Given a non-empty 2D array grid of 0's and 1's,
 * an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.)
 * You may assume all four edges of the grid are surrounded by water.
Count the number of distinct islands.
An island is considered to be the same as another if and only if one island can be translated (and not rotated or reflected) to equal the other.
Example 1:
11000
11000
00011
00011
Given the above grid map, return 1.

Example 2:
11011
10000
00001
11011
Given the above grid map, return 3.

Notice that:
11
1
and
1
11
are considered different island shapes, because we do not consider reflection / rotation.

Note: The length of each dimension in the given grid does not exceed 50.
 * */
class Solution {
    /*
    * solution: bfs, count the number of island, then remove the same sharp via HashSet,
    * Time complexity:O(mn), Space complexity:O(mn)
    * */

    val set = HashSet<ArrayList<Int>>()
    val directions = intArrayOf(0, 1, 0, -1, 0)

    fun numDistinctIslands(grid: Array<IntArray>): Int {
        if (grid == null || grid.isEmpty()) {
            return 0
        }
        val m = grid.size
        val n = grid[0].size
        for (i in 0 until m) {
            for (j in 0 until n) {
                if (grid[i][j] == 1) {
                    bfs(grid, i, j)
                }
            }
        }
        return set.size
    }

    private fun bfs(grid: Array<IntArray>, x: Int, y: Int) {
        val queue = LinkedList<Pair<Int, Int>>()
        queue.offer(Pair(x, y))
        //set as visited
        grid[x][y] = -1
        //list for save index of directions
        val list = ArrayList<Int>()
        while (queue.isNotEmpty()) {
            val cur = queue.pop()
            for (d in 0 until 4) {
                val newX = cur.first + directions[d]
                val newY = cur.second + directions[d + 1]
                if (newX < 0 || newY < 0 || newX >= grid.size || newY >= grid[0].size || grid[newX][newY] != 1) {
                    continue
                }
                //set as visited
                grid[newX][newY] = -1
                queue.offer(Pair(newX, newY))
                list.add(d)
            }
        }
        set.add(list)
    }
}
复制代码

 

posted @   johnny_zhao  阅读(73)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示