zoukankan      html  css  js  c++  java
  • 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)
        }
    }
  • 相关阅读:
    sikiA计划问题记录
    列表+泛型
    查看别人项目找代码的方法
    Unity3d 异常与解决方案集合(持续)
    实现继承+接口继承+虚方法+隐藏方法+this/base+抽象类+密封类/方法+修饰符
    定义类+类实例化+属性+构造函数+匿名类型var+堆与栈+GC回收机制+值类型与引用类型
    局部变量和成员变量的区别
    数组元素二分查找(折半查找)
    数组元素冒泡排序
    数组元素选择排序
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/14011969.html
Copyright © 2011-2022 走看看