leetcode 840. Magic Squares In Grid

LeetCode --- 840. Magic Squares In Grid 解题报告 A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbersfrom 1 to 9such that each row, column, and both diagonals all have the same sum. Given angridof integers, how many 3 x 3 "magic square" subgrids are there? (Each subgrid is contiguous).... 阅读详情

 

一、题目解析

3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等。

给定一个由整数组成的 N × N 矩阵,其中有多少个 3 × 3 的 “幻方” 子矩阵?(每个子矩阵都是连续的)。

 

示例 1:

输入: [[4,3,8,4],
      [9,5,1,9],
      [2,7,6,2]]
输出: 1
解释: 
下面的子矩阵是一个 3 x 3 的幻方:
438
951
276

而这一个不是:
384
519
762

总的来说,在本示例所给定的矩阵中只有一个 3 x 3 的幻方子矩阵。

提示:

  1. 1 <= grid.length = grid[0].length <= 10
  2. 0 <= grid[i][j] <= 1

二、解法分析

   幻方的几个要素 (也就是3*3的矩阵):

1、中心元素必定为5,

2、其实这个幻方一共有8中,只要有一个解,其他的解可以通过矩阵变换来的得到,比如:这个幻方  

 

438
951
276

 我们可以通过对矩阵转置,就可以得到另外一个幻方,

3、也可以通过顺时针旋转90度,得到另外一个解。

利用这三个要素就可以解决本题了,步骤如下:

    1、在数据中寻找中心元素5

    2、判断以这个中心元素为5的3*3的子矩阵是否为幻方

 

代码如下:

class Solution {
public:
    
    int numMagicSquaresInside(vector<vector<int>>& grid) {
        if(grid[0].size() < 3 || grid.size() < 3)
            return 0;
        int cnt = 0;
        for(int i = 1; i < grid.size() - 1; i++){
            for(int j = 1; j < grid[i].size() - 1; j++){
                if(grid[i][j] == 5){                              //判断中心元素是否为5
                    if(isMagic(grid, i, j))                      //判断是否为幻方
                        cnt++;
                }
            }
        }
        return cnt;
    }
    bool isMagic(vector<vector<int>> &grid, int i, int j){
        vector<vector<int>> magic(3,vector<int>(3));
        magic[0][0] = 4;
        magic[0][1] = 3;
        magic[0][2] = 8;
        magic[1][0] = 9;
        magic[1][1] = 5;
        magic[1][2] = 1;
        magic[2][0] = 2;
        magic[2][1] = 7;
        magic[2][2] = 6;                              //magic代表一个幻方
        for(int k = 0; k < 4; k++){
            rotating(magic);                          //顺时针旋转90度后,形成另一个幻方
            if(grid[i-1][j-1] == magic[0][0] && grid[i-1][j] == magic[0][1] && grid[i-1][j+1] == magic[0][2] && grid[i][j-1] == magic[1][0] && grid[i][j + 1] == magic[1][2] && grid[i+1][j-1] == magic[2][0] && grid[i+1][j] == magic[2][1] && grid[i+1][j+1] == magic[2][2])
                return true;
            sym(magic);                               //转秩后,形成另一个幻方
            if(grid[i-1][j-1] == magic[0][0] && grid[i-1][j] == magic[0][1] && grid[i-1][j+1] == magic[0][2] && grid[i][j-1] == magic[1][0] && grid[i][j + 1] == magic[1][2] && grid[i+1][j-1] == magic[2][0] && grid[i+1][j] == magic[2][1] && grid[i+1][j+1] == magic[2][2])
                return true;
            sym(magic);                                
        }
        return false;
    }
    void sym(vector<vector<int>> &mt){
        for(int i = 0; i < 3; i++){
            for(int j = i; j < 3; j++){
                if(j != i){
                    int tmp = mt[i][j];
                    mt[i][j] = mt[j][i];
                    mt[j][i] = tmp;
                }
            }
        }
    }
    void rotating(vector<vector<int>> &mt){
        for(int i = 0; i < 3; i++){
            for(int j = i; j < 3; j++){
                if(j != i){
                    int tmp = mt[i][j];
                    mt[i][j] = mt[j][i];
                    mt[j][i] = tmp;
                }
            }
        }
        for(int i = 0 ; i < 3; i++){
            int tmp = mt[i][0];
            mt[i][0] = mt[i][2];
            mt[i][2] = tmp;
        }
    }
    
};

运行时间:4ms

Redis【实战篇】---- 分布式锁-redission 重入问题:重入问题是指 获得锁的线程可以再次进入到相同的锁的代码块中,可重入锁的意义在于防止死锁,比如HashTable这样的代码中,他的方法都是使用synchronized修饰的,假如他在一个方法内,调用另一个方法,那么此时如果是不可重入的,不就死锁了吗?所以可重入锁他的主要意义是防止死锁,我们的synchronized和Lock锁都是可重入的。不可重试:是指目前的分布式只能尝试一次,我们认为合理的情况是:当线程在获得锁失败后,他应该能再次尝试获得锁。 阅读详情

相关推荐

如何用capl操控程控电源(RS232)

capl脚本操控RS232

xiaohuanxiong_的博客 3991

LeetCode - 840 - 矩阵中的幻方(magic-squares-in-grid)

一 目录 不折腾的前端,和咸鱼有什么区别目录一 目录二 前言三 解题及测试四 LeetCode Submit五 解题思路六 进一步思考二 前言 难度:简单涉及知识:数组题目地址:http...

weixin_41806099的博客 252

《深入浅出图神经网络:GNN原理解析》学习笔记

期末考完了,该上道了 目录一、图的概述1.基本概念2.图的存储和遍历3.应用分类二、神经网络基础1.机器学习2.神经网络三、卷积神经网络(CNN)1.卷积与池化2.卷积神经网络四、表示学习1.意义2.自编码器五、图信号处理与图卷积神经网络1.矩阵计算前置知识2.图信号与图的拉普拉斯矩阵3.图傅里叶变换4.图滤波器(Graph Filter)5.图卷积神经网络(GCN) 一、图的概述 1.基本概念 1.顶点(Vertex)表示研究的对象,边(Edge)表示两个对象之间特定的研究关系 2.图为顶点和边的集合.

且视他人之疑目如盏盏鬼火,大胆地去走你的夜路 2049

Magic Squares In Grid

A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbersfrom 1 to 9such that each row, column, and both diagonals all have the same sum. Given angridof integers, how many 3 x 3 "magic square" subgrids are there? (Each subgrid is contiguous)....

leetcode 解题思路 300

BAPI创建委外采购订单

使用BAPI BAPI_PO_CREATE1创建委外采购订单,组件为手工输入时,有几个点需要注意: 1、已通过CS01创建过BOM的物料,创建时会自动带出components。如果要修改当前组件清单,可以通过设置pocomponents-change_id = 'E’来实现 lv_sline = lv_sline + 1. ls_pocomponents-po_item = l_ebelp + 10. ls_pocomponents-ITEM_NO = l

Buffalo_soldier的博客 3261

学习海康VisionMaster之表面缺陷滤波

本文介绍了VisionMaster中表面缺陷滤波技术的应用。该技术通过数学变换抑制背景干扰、增强缺陷特征,适用于电池铝壳、垫圈/螺丝等无纹理背景的表面缺陷检测(划痕、凹坑、异物等)。操作流程包括:图像导入、轮廓定位、位置修正、参数设置(重点讲解滤波核尺寸/数量/标准差等关键参数的调整策略)。文章通过案例演示了如何通过多方向滤波响应识别表面划伤,并指出需结合blob分析进一步优化检测结果。该技术对工业质检具有实用价值,参数设置需结合缺陷特征与背景特性灵活调整。

yuhouxiyang的专栏 2828

【LeetCode】840. Magic Squares In Grid 解题报告(Python)

【LeetCode】840. Magic Squares In Grid 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/magic-squares-in-grid/description/ 题目描述: A 3 x 3 magic square is a 3 x 3 grid filled wit...

负雪明烛 2919

JVM高频面试基本问题与知识点整理

JVM高频面试基本问题与知识点整理

曾经“等你生日那天”都遥远得像未来,如今却可欢愉的挥手说“下个十年见” 87万+

LeetCode--840. Magic Squares In Grid

A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, column, and both diagonals all have the same sum.Given an grid of integers, how many 3 x 3 "magic squa...

vir_lee的博客 509

基于MATLAB的PDT DMR物理层仿真

PDT DMR物理层仿真的目标是评估系统的误码率(Bit Error Rate,BER),以及验证和优化各种调制方案、编码方案和信道模型。下面将介绍如何进行PDT DMR物理层的仿真,并提供相应的MATLAB源代码。通过上述步骤,我们可以进行基于MATLAB的PDT DMR物理层仿真,并评估系统的性能。根据实际需求,我们可以修改仿真参数和添加更复杂的信道模型,以更全面地评估系统性能。最后,我们可以将仿真结果进行可视化和分析。首先,我们需要设置仿真的参数,包括调制方案、编码方案、信道模型和信噪比等。

TechPulseZ的博客 459

Matlab 普通kriging(克里金)插值

Matlab 普通kriging(克里金)插值

dayuhaitang1的博客 4571

[LeetCode]840. Magic Squares In Grid 解题报告(C++)

[LeetCode]840. Magic Squares In Grid 解题报告(C++) 题目描述 A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, column, and both diagonals all have the same s...

【黑键】 611

Neo4j:入门基础(二)之导入CSV文件

导入CSV文件的方式  主要的两种方式如下:   1、使用 Cypher 命令LOAD CSV,这种方式可以处理中小型数据(可达10 million条记录)   2、使用批量导入工具neo4j-admin ,用来直接导入大数据集 三、上述方式具体导入过程   1、Cypher 命令LOAD CSV 参数 1、USING PERIODIC COMMIT 300 使用批量提交,每满300条提交一次,防止内存溢出 2、WITH HEADERS 从文件中读取第一行作为参数名,...

Puzzle的博客 3万+

[leetcode] 840. Magic Squares In Grid

Description A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, column, and both diagonals all have the same sum. Given an grid of integers, how many 3 x ...

Learning from the mistakes 337

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

Leetcode【840】Magic Squares In Grid

python3暴力解 class Solution(object): def numMagicSquaresInside(self, grid): """ :type grid: List[List[int]] :rtype: int """ grids = self.generate_matrix(gri...

ssjdoudou的博客 7354

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

LeetCode //C - 840. Magic Squares In Grid

This problem involves counting the number of 3×3 magic square subgrids in a given grid. A magic square must contain distinct numbers from 1 to 9, with each row, column, and both diagonals summing to 15. The center of the square must be 5. The solution chec

Made in Code 742

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

【Leetcode_easy】840. Magic Squares In Grid

problem 840.Magic Squares In Grid 参考 1. Leetcode_easy_840.Magic Squares In Grid; 完 转载于:https://www.cnblogs.com/happyamyhope/p/11214881.html

weixin_30540691的博客 119

LeetCode 840. Magic Squares In Grid 找规律

A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, column, and both diagonals all have the same sum. Given an gridof integers, how many 3 x 3 "magic square" subgrids are there? (Each subgrid is contiguous)...

taoqick的专栏 400

Leetcode 840. Magic Squares In Grid

文章作者:Tyan 博客:noahsnail.com  |  优快云  |  简书 1. Description 2. Solution **解析:**根据三阶幻方的定义,一项项检查即可,满足条件+1。Version 1是针对3*3的,Version 2进行了一些修改,减少了代码量,Version 3是针对k*k的通用版本。 Version 1 class Solution: def numMagicSquaresInside(self, grid:

SnailTyan 192

【Leetcode】840. Magic Squares In Grid

题目地址: https://leetcode.com/problems/magic-squares-in-grid/ 给定一个m×nm\times nm×n的矩阵,问有多少个3×33\times 33×3的方阵是333阶幻方。 直接按定义判断即可。代码如下: import java.util.HashSet; import java.util.Set; public class Solution { public int numMagicSquaresInside(int[][] grid) {

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

Leetcode840.Magic Squares In Grid矩阵中的幻方

3 x 3 的幻方是一个填充有从 1 到 9的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等。 给定一个由整数组成的 N × N 矩阵,其中有多少个 3 × 3 的 “幻方” 子矩阵?(每个子矩阵都是连续的)。 示例 1: 输入: [[4,3,8,4], [9,5,1,9], [2,7,6,2]] 输出: 1 ...

Ha12312的博客 360

[Swift]LeetCode840. 矩阵中的幻方 | Magic Squares In Grid

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文地址:https://www.cnblogs.com/stren...

weixin_30362083的博客 142

英伟达DLSS5怎么开?从显卡驱动到游戏设置的全流程实操指南

在游戏画质与帧率难以兼得的当下,深度学习超采样(DLSS)技术正成为玩家提升性能的关键手段。从早期的CNN架构到如今的Transformer模型,AI渲染技术不断进化,多帧生成与光线重建等特性让画面流畅度与细节表现实现了质的飞跃。然而,要真正发挥新一代DLSS的潜力,不仅需要理解其底层原理,还需掌握硬件选型、驱动更新、游戏内设置等一系列工程实践技巧。本文从显卡架构、显存需求到组件替换的完整链条出发,结合《燕云十六声》《赛博朋克2077》等热门大作的实测数据,深入解析开启DLSS5的正确姿势,并针对性解答文件

weixin_33904756的博客 356

leetcode 840. Magic Squares In Grid 矩阵中的幻方-耗时100%

邹九的个人博客 372

[LeetCode] 840. Magic Squares In Grid_Easy

A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbersfrom 1 to 9such that each row, column, and both diagonals all have the same sum. Given angridof integers, how many 3 x 3 "magic sq...

weixin_30794851的博客 185
上一篇: Leetcode:239. Sliding Window Maximum (c++)
下一篇: leetcode 836 矩形重叠
Torch_Man
博客等级 码龄9年 6粉丝 25原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值