[Leetcode] 711. Number of Distinct Islands II 解题报告

python数组排序方法详解(sort, sorted,argsort) 这三个排序方法应对日常工作基本够用 先说一下三者的区别 sort, sorted 是用在 list 数据类型中的排序方法 argsort 是用在 numpy 数据类型中的排序方法( numpy 里也有一个 sort 方法,下面会讲) sort 和 sorted 的区别如下???? 先看两个简单的升序排序,分别使用 sorted 和 sort 方法 # sorted num_list = [1, 8, 2, 3, 10, 4, 5] ordered_list = sorted(num_list) print 阅读详情

题目:

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 they have the same shape, or have the same shape after rotation (90, 180, or 270 degrees only) or reflection (left/right direction or up/down direction).

Example 1:

11000
10000
00001
00011
Given the above grid map, return  1 . 

Notice that:
11
1
and
 1
11
are considered  same  island shapes. Because if we make a 180 degrees clockwise rotation on the first island, then two islands will have the same shapes.

Example 2:

11100
10001
01001
01110
Given the above grid map, return  2 .

Here are the two distinct islands:
111
1
and
1
1

Notice that:
111
1
and
1
111
are considered  same  island shapes. Because if we flip the first array in the up/down direction, then they have the same shapes.

Note: The length of each dimension in the given grid does not exceed 50.

思路:

DFS的套路不难,这里主要是如何处理小岛的旋转问题。我们的做法是:每次找到一个小岛之后,我们生成其8个对应的变形(包括本身,翻转以及旋转等)。然后返回其排序最小的那个,作为这种类型的小岛的代表。这样其余的思路就都可以照搬 [Leetcode] 694. Number of Distinct Islands 解题报告中的思路了。

代码:

class Solution {
public:
    int numDistinctIslands2(vector<vector<int>>& grid) {
        int row_num = grid.size(), col_num = grid[0].size();
        set<vector<pair<int, int>>> islands;
        for (int r = 0; r < row_num; ++r) {
            for (int c = 0; c < col_num; ++c) {
                if (grid[r][c] == 1) {
                    vector<pair<int, int>> island;
                    DFS(grid, r, c, r, c, island);
                    islands.insert(normalize(island));
                }
            }
        }
        return islands.size();
    }
private:
    void DFS(vector<vector<int>> &grid, int r0, int c0, int r, int c, vector<pair<int, int>> &island) {
        int row_num = grid.size(), col_num = grid[0].size();
        if (r < 0 || r >= row_num || c < 0 || c >= col_num || grid[r][c] <= 0) {
            return;
        }
        grid[r][c] = -1;
        island.push_back(make_pair(r - r0, c - c0));
        for (int d = 0; d < 4; ++d) {
            DFS(grid, r0, c0, r + delta[d][0], c + delta[d][1], island);
        }
    }
    vector<pair<int, int>> normalize(vector<pair<int, int>> &a) {
        vector<vector<pair<int, int>>> ret(8, vector<pair<int, int>>());
        for (auto &p : a) {
            int x = p.first, y = p.second;
            ret[0].push_back(make_pair(x, y));
            ret[1].push_back(make_pair(x, -y));
            ret[2].push_back(make_pair(-x, y));
            ret[3].push_back(make_pair(-x, -y));
            ret[4].push_back(make_pair(y, x));
            ret[5].push_back(make_pair(y, -x));
            ret[6].push_back(make_pair(-y, x));
            ret[7].push_back(make_pair(-y, -x));
        }
        for (int i = 0; i < 8; ++i) {
            sort(ret[i].begin(), ret[i].end());
            int r_offset = 0 - ret[i][0].first, c_offset = 0 - ret[i][0].second;
            for (int j = 0; j < ret[i].size(); ++j) {
                ret[i][j].first += r_offset;
                ret[i][j].second += c_offset;
            }
        }
        sort(ret.begin(), ret.end());
        return ret[0];
    } 
    int delta[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
};

<PLC><socket><汇川>汇川PLC使用socket与其他控制器(如上位机)通讯模板程序示例 以上介绍了汇川PLC通用socket通讯的示例,事实上,汇川的官方手册也有使用示例,可以直接仿照使用,也可以加以修改。 阅读详情

相关推荐

Ceres-solver examples之pose_graph_3d学习笔记

Ceres-solver examples之pose_graph_3d学习笔记简要说明变量说明关键步骤一、Costfunction的搭建其中包括:二、构造Problem 简要说明 ceres-solver库是google的非线性优化库,可以对slam问题,机器人位姿进行优化,使其建图的效果得到改善。pose_graph_3d是官方给出的二维平面上机器人位姿优化问题,需要读取一个g2o文件,运行程序...

幽冥小龙的博客 3888

Python中list和tuple的相互转换

(1)list 转为 tuple: temp_list = [1,2,3,4,5] print(temp_list) print(type(temp_list)) a = tuple(temp_list) #将temp_list进行强制转换 print(a) print(type(a)) #查看是否转换成功 (2)tuple 转为 list: temp_tupl...

dev_zyx的博客 2万+

树莓派4B:控制步进电机

文章目录准备阶段接线阶段树莓派程序 准备阶段 准备以下物品,淘宝都可以买到: 57步进电机 电源开关(220v转24v,3A,72W) 步进电机驱动器 电源线 电线 杜邦线 树莓派4B一套 具体型号如下: 接线阶段 整体:接线效果如下图 电源开关起220v转24v作用,很好接线,输出24v接入步进电机驱动器 步进电机驱动器的 A+ A- B+ B- 按提示接步进电机四线,步进电机上就会有说...

学习路上的鸭子的博客 1万+

【LeetCode 深度优先搜索专项】不同岛屿的数量 II(711)

【LeetCode 深度优先搜索专项】不同岛屿的数量 II(711)

weixin_37780776的博客 1011

新手如何学习车载底层嵌入式软件开发(AutoSar)

汽车嵌入式开发

ACarSoft的博客 4219

OPPO 后端校招面试,过于简单了!

你好,我是 Guide。2026 届秋招陆续开奖公司的越来越多了,今天来看一家手机厂 OPPO。前几天,已经分享了和今年校招开奖的情况。其中,小红书的薪资最为炸裂,大白菜一年都能给到 51w+。不过,相对应的工作强度和压力也是真的难顶,确实很难有钱多又轻松的活!OV (OPPO 和 Vivo)这几年发展真还可以的,这两家的高端机口碑都不错。本人也是最近入手了一款 Vivo 的上一代旗舰,拍照是真的不错啊!每个月会有 1.2k 生活补贴,生活费是按照 8 % 缴纳。

JavaGuide 748

leetcode:711.不同岛屿的数量 II

如果两个岛屿之间形状相同,或者旋转(旋转角度仅可以为 90 、 180或 270 度)之后形状相同,或者镜像(可以沿上下或左右镜像)之后形状相同,就称这两个岛屿是相同的。给定一个 m × n的二维矩阵 grid ,使用或上、或下、或左、或右方向上相接的一组 1 代表一个岛屿。可以假定矩阵以外的区域都是海水。要求返回给定 grid 中包含的不同岛屿数量。

OceanStar的博客 1161

汽车ECU诊断刷写和OTA升级中的验签和校验

汽车ECU诊断刷写和OTA升级中的验签和校验

天赐好车的博客 615

python对象排序

print(sorted(var,key=lambda a:a['value'],reverse=True)) #根据指定字段排序。sorted_list = sorted(数组, key=lambda 键: 键.去对应的值,reverse=True)sorted_list = sorted(数组, key=lambda 键: 键.去对应的值)print(sorted(var,key=lambda a:a[2])) #根据指定角标排序。print(sorted(var)) #正序。

Meseiter的博客 1172

软件可靠性测试与评价

本文系统介绍了软件可靠性工程的核心内容,包括可靠性概念、模型分类、测试方法和评价体系。重点解析了可靠性测试的三大步骤:定义运行剖面、设计测试用例和实施测试收集数据,并以智能安防系统为例说明具体应用。可靠性评价需结合业务场景,通过量化指标(MTBF、失效率等)和模型预测持续优化系统。文章强调可靠性需贯穿软件全生命周期,通过闭环反馈实现持续改进,为关键系统提供质量保障。

moton2017的博客 2048

PyTorch强化学习实战(1)——强化学习(Reinforcement Learning,RL)详解

在本节中,开始进入探索强化学习 (Reinforcement Learning, RL) 之旅,首先剖析了强化学习的独特之处及其与监督学习、无监督学习范式的关系。随后系统学习了强化学习的基础形式化框架及其内部关联机制,并深入讲解了马尔可夫过程 (Markov Process, MP)、马尔可夫奖励过程 (Markov Reward Process, MRP) 和马尔可夫决策过程 (Markov Decision Process, MDP),为后续学习奠定基础。

盼小辉丶的博客 2050

【Leetcode】711. Number of Distinct Islands II

题目地址: https://leetcode.com/problems/number-of-distinct-islands-ii/ 给定一个mmm行nnn列的0−10-10−1矩阵,每个111连通块视为一个岛屿。这里的连通是四连通。两个岛屿相同当且仅当其中一个可以通过旋转0°,90°,180°,270°0^\degree,90^\degree,180^\degree,270^\degree0°,90°,180°,270°或者镜面翻转后,与另一个岛屿重合。问一共有多少个不同的岛屿。 可以用哈希。这里需要保证

数学、算法爱好者的博客 719

PINN求解PDE

PINN求解Dirichlet泊松方程 泊松方程引入 −Δu=f,x∈Ω=(0,1)2-\Delta u=f,x \in \Omega=(0,1)^2−Δu=f,x∈Ω=(0,1)2 u=g,x∈∂Ωu=g,x \in \partial \Omegau=g,x∈∂Ω import torch import time import numpy as np import matplotlib.pyplot as plt import torch.nn as nn def UU(X, order,prob):

forrestguang的博客 3804

【LeetCode】Number of Islands 解题报告

【题目】 Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. Yo

跳出温水的青蛙 1万+

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

[LeetCode] Number of Islands II

Given a n,m which means the row and column of the 2D matrix and an array of pair A( size k). Originally, the 2D matrix is all 0 which means there is only sea in the matrix. The list pair has k ope...

weixin_33835690的博客 178

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

[Leetcode] 694. Number of Distinct Islands 解题报告

题目: 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 gr

魔豆(Magicbean)的博客 2168

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

LeetCode711. Number of Distinct Islands II - - 彻底掌握并查集(Union Find)系列题8

​ 这道题是LeetCode694. Number of Distinct Islands的延伸。对于求岛屿个数已经没有任何难度了,直接套用并查集(Union Find)模板即可。 题目被标为hard是因为题目对相同岛屿的定义使得判断两个岛屿是否相同的难度加大,

hgq522的博客 392

[LeetCode305]Number of Islands II

Hard..A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to op

MoM 975

[LeetCode] 711. Number of Distinct Islands II_hard tag: DFS

Given a non-empty 2D arraygridof 0's and 1's, anislandis a group of1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrou...

weixin_30699741的博客 112

[LeetCode] Number of Distinct Islands II 不同岛屿的个数之二

  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 sur...

weixin_33851177的博客 282

Leetcode总结(Island专题)

Leetcode总结之 Island专题 Table of Contents 总结 200.Number of Islands 305.Number of Islands II 695.Max Area of Island 130. Surrounded Regions 694. Number of Distinct Islands 711. Number of Distinct Islands II 总结 Island专题基本上是图的题目,方法主要是DFS, BFS和...

Aaron_Marvel的博客 447

Number of Islands II Leetcode

A 2d grid map ofmrows andncolumns is initially filled with water. We may perform anaddLandoperation which turns the water at position (row, col) into a land. Given a list of positions to operate...

weixin_30851409的博客 138

[LeetCode] Number of Distinct Islands 不同岛屿的个数

Given a non-empty 2D arraygridof 0's and 1's, anislandis a group of1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are sur...

weixin_30469895的博客 92

Number of Distinct Islands - LintCode

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 surrou...

kane的博客 438

Python里面如何实现tuple和list的转换?

在这个示例中,原始元组tuple_var被修改为(4, 5, 6),但转换后的列表list_var没有受到影响,它仍然保持原始的元素[1, 2, 3],并追加了一个新的元素4。这表明转换后的对象是独立的,修改其中一个不会影响到另一个。需要注意的是,转换后的对象是新的数据结构,与原始的元组或列表是独立的,修改其中一个不会影响到另一个。另外,如果元组或列表中的元素是可变的,转换后的对象仍然保持对原始元素的引用。上述代码示例中,list()函数用于将元组转换为列表,tuple()函数用于将列表转换为元组。

zy1992As的博客 4584

python数组排序的方法及常用代码

简单排序是指根据数组中元素的大小,依次对数组中的元素进行排序的方法。简单排序的基本思想是:每次从最小的元素开始,向上或向下取一个最大的元素,然后再从这个最大的元素开始,向上或向下取一个最小的元素,依此类推,直到完成所有元素的排序。在使用插入排序时,我们需要注意以下几点: 1、如果要对数组进行排序,我们需要先将数组拆分成一个个小的数组,然后再对每个小数组排序数组中的元素。冒泡排序是一种高效的排序方法,其思想是将一个数组按照序列的方式进行排序,并将最后一个元素移动到最左边的位置,这个方法不需要进行数据的操作。

花生日记 8423
上一篇: [Leetcode] 699. Falling Squares 解题报告
下一篇: [Leetcode] 712. Minimum ASCII Delete Sum for Two Strings 解题报告
魔豆Magicbean
博客等级 码龄19年 146粉丝 742原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值