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 square" subgrids are there? (Each subgrid is contiguous).
Example 1:
Input: [[4,3,8,4],
[9,5,1,9],
[2,7,6,2]]
Output: 1
Explanation:
The following subgrid is a 3 x 3 magic square:
438
951
276 while this one is not:
384
519
762 In total, there is only one magic square inside the given grid.
Note:
1 <= grid.length <= 10
1 <= grid[0].length <= 10
0 <= grid[i][j] <= 15
要完成的函数:
int numMagicSquaresInside(vector<vector<int>>& grid)
说明:
1、这道题给定一个二维的vector,也就是一个矩阵,要求判断这个矩阵中含有多少个magic三维矩阵,返回数量。
magic三维矩阵的定义是,所有数值都在1-9之间(闭区间),且每一行、每一列和每条对角线的和都相等(也就是15)。
2、这是一道简单题,我们直接暴力处理就好了。
首先,我们判断每个子矩阵的中心点是不是为5。这一步可以过滤掉很多子矩阵。
接着,判断符号第一个条件的子矩阵的所有9个数,是不是都在1-9之间。在测试样例中,出现了包含0和10这样的数的子矩阵,也可以形成magic矩阵。
最后,判断三行、三列和两条对角线的和是否为15。
代码构造如下:(附详解)
int numMagicSquaresInside(vector<vector<int>>& grid)
{
int hang=grid.size(),lie=grid[0].size(),res=0;
for(int i=1;i<=lie-2;i++)
{
for(int j=1;j<=lie-2;j++)//从第二行第二列开始判断中心点,直到倒数第二行倒数第二列结束
{
if(grid[i][j]==5)//满足第一个条件
{
if(grid[i-1][j-1]<=9&&grid[i-1][j-1]>=1&&
grid[i-1][j]<=9&&grid[i-1][j]>=1&&
grid[i-1][j+1]<=9&&grid[i-1][j+1]>=1&&
grid[i][j-1]<=9&&grid[i][j-1]>=1&&
grid[i][j+1]<=9&&grid[i][j+1]>=1&&
grid[i+1][j-1]<=9&&grid[i+1][j-1]>=1&&
grid[i+1][j]<=9&&grid[i+1][j]>=1&&
grid[i+1][j+1]<=9&&grid[i+1][j+1]>=1)//判断是否都在1-9之间
{
if((grid[i-1][j-1]+grid[i-1][j]+grid[i-1][j+1]==15)&&
(grid[i][j-1]+grid[i][j]+grid[i][j+1]==15)&&
(grid[i+1][j-1]+grid[i+1][j]+grid[i+1][j+1]==15)&&
(grid[i-1][j-1]+grid[i][j-1]+grid[i+1][j-1]==15)&&
(grid[i-1][j]+grid[i][j]+grid[i+1][j]==15)&&
(grid[i-1][j+1]+grid[i][j+1]+grid[i+1][j+1]==15)&&
(grid[i-1][j-1]+grid[i][j]+grid[i+1][j+1]==15)&&
(grid[i-1][j+1]+grid[i][j]+grid[i+1][j-1]==15))//判断三行三列和两条对角线的和是否为15
res++;
}
}
}
}
return res;
}
上述代码实测5ms,因为服务器没有充分的提交量,所以没有打败的百分比。
leetcode-840-Magic Squares In Grid的更多相关文章
- 【Leetcode_easy】840. Magic Squares In Grid
problem 840. Magic Squares In Grid solution: class Solution { public: int numMagicSquaresInside(vect ...
- 840. Magic Squares In Grid (5月27日)
开头 这是每周比赛中的第一道题,博主试了好几次坑后才勉强做对了,第二道题写的差不多结果去试时结果比赛已经已经结束了(尴尬),所以今天只记录第一道题吧 题目原文 Magic Squares In Gri ...
- 【LeetCode】840. Magic Squares In Grid 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 利用河图规律 暴力解法 日期 题目地址:https: ...
- [LeetCode] 840. Magic Squares In Grid_Easy
A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, co ...
- 840. Magic Squares In Grid ——weekly contest 86
题目链接:https://leetcode.com/problems/magic-squares-in-grid/description attention:注意给定的数字不一定是1-9. time: ...
- 840. Magic Squares In Grid
class Solution { public: int numMagicSquaresInside(vector<vector<int>>& grid) { ; in ...
- [LeetCode] 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, co ...
- LeetCode算法题-Magic Squares In Grid(Java实现)
这是悦乐书的第326次更新,第349篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第196题(顺位题号是840).3 x 3魔方是一个3 x 3网格,填充了从1到9的不同 ...
- C#LeetCode刷题之#840-矩阵中的幻方(Magic Squares In Grid)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3752 访问. 3 x 3 的幻方是一个填充有从 1 到 9 的不 ...
- [Swift]LeetCode840. 矩阵中的幻方 | 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, co ...
随机推荐
- Win10 Cygwin Cd Permission denied
问题描述 在win10或者win系统上面,使用cygwin的时候, 有时候会出现, 权限问题. 即使是管理员也不行. 问题分析 这个问题,我个人觉得,主要是使用不当造成的. 也就是说, 可能使用chm ...
- macOS 安装设置 Tunnelblick (openvpn 替代)
1、下载.安装 2、准备好 openvpn 相关的文件,全部放在一个目录中.主要有以下几个: ca.crt client.crt client.key client.ovpn 3、打开 client. ...
- ResourceUtils读取properties文件
注意: properties文件要放在classPath下面,也就是与src下. path.properties(在properties文件中\代表着没有完,下行同本行是一个内容) perfectMa ...
- 看图说话:关于BI那点事儿
[编者按]BI=DW+数据挖掘+业务分析+社会学?BI三部曲:管数据.看数据.源数据.BI有三种放法:技术部.业务部和独立部门.BI的工作=20%数据平台+30%数据支持+50%数据应用.
- 数据结构(c语言版)文摘
第一章 绪论 数据结构:是一门研究非数值计算的程序设计问题中计算机的操作对象以及它们之间的关系和操作等的学科. 数据:是对客观事物的符号表示,在计算机科学中是指所有能输入到计算机中并被计算机程序处理 ...
- meterpreter命令
meterpreter详解与渗透实战 基本命令: background quit shell irb client.sys.config.sysinfo() 调用windows API:client. ...
- Ubuntu 14.04 install emacs 24.5
1.前期准备工作 2.安装基础构件工具 3.下载emacs编译需要的依赖库 4.下载emacs24.5编译安装 5.下载并安装我的emacs配置文件 6.配置tmux和zsh 1. 前期准备工作 在阿 ...
- Web 协议 HTTP1.0 HTTP1.1 SPDY HTTP2.0
Web 协议 HTTP1.0 HTTP1.1 SPDY HTTP2.0 HTTP1.0 VS HTTP1.1 长连接HTTP 1.0需要使用keep-alive参数来告知服务器端要建立一个长连接,而H ...
- 存储系统的基本数据结构之一: 跳表 (SkipList)
在接下来的系列文章中,我们将介绍一系列应用于存储以及IO子系统的数据结构.这些数据结构相互关联又有着巨大的区别,希望我们能够不辱使命的将他们分门别类的介绍清楚.本文为第一节,介绍一个简单而又有用的数据 ...
- 利用backgroundwork----递归读取网页源代码,并下载href链接中的文件
今天闲着没事,研究了一下在线更新程序版本的问题.也是工作中的需要,开始不知道如何下手,各种百度也没有找到自己想要的,因为我的需求比较简单,所以就自己琢磨了一下.讲讲我的需求吧.自己在IIs上发布了一个 ...