[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, 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 <= 101 <= grid[0].length <= 100 <= grid[i][j] <= 15
这道题定义了一种神奇正方形,是一个3x3大小,且由1到9中到数字组成,各行各列即对角线和都必须相等。那么其实这个神奇正方形的各行各列及对角线之和就已经被限定了,必须是15才行,而且最中间的位置必须是5,否则根本无法组成满足要求的正方形。博主也没想出啥特别巧妙的方法,就老老实实的遍历所有的3x3大小的正方形呗,我们写一个子函数来检测各行各列及对角线的和是否为15,在调用子函数之前,先检测一下中间的数字是否为5,是的话再进入子函数。在子函数中,先验证下该正方形中的数字是否只有1到9中的数字,且不能由重复出现,使用一个一维数组来标记出现过的数字,若当前数字已经出现了,直接返回true。之后便是一次计算各行各列及对角线之和是否为15了,若全部为15,则返回true,参见代码如下:
class Solution {
public:
int numMagicSquaresInside(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[].size(), res = ;
for (int i = ; i < m - ; ++i) {
for (int j = ; j < n - ; ++j) {
if (grid[i + ][j + ] == && isValid(grid, i, j)) ++res;
}
}
return res;
}
bool isValid(vector<vector<int>>& grid, int i, int j) {
vector<int> cnt();
for (int x = i; x < i + ; ++x) {
for (int y = j; y < j + ; ++y) {
int k = grid[x][y];
if (k < || k > || cnt[k] == ) return false;
cnt[k] = ;
}
}
if ( != grid[i][j] + grid[i][j + ] + grid[i][j + ]) return false;
if ( != grid[i + ][j] + grid[i + ][j + ] + grid[i + ][j + ]) return false;
if ( != grid[i + ][j] + grid[i + ][j + ] + grid[i + ][j + ]) return false;
if ( != grid[i][j] + grid[i + ][j] + grid[i + ][j]) return false;
if ( != grid[i][j + ] + grid[i + ][j + ] + grid[i + ][j + ]) return false;
if ( != grid[i][j + ] + grid[i + ][j + ] + grid[i + ][j + ]) return false;
if ( != grid[i][j] + grid[i + ][j + ] + grid[i + ][j + ]) return false;
if ( != grid[i + ][j] + grid[i + ][j + ] + grid[i][j + ]) return false;
return true;
}
};
参考资料:
https://leetcode.com/problems/magic-squares-in-grid/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Magic Squares In Grid 网格中的神奇正方形的更多相关文章
- Leetcode840.Magic Squares In Grid矩阵中的幻方
3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个由整数组成的 N × N 矩阵,其中有多少个 3 × 3 的 & ...
- 840. Magic Squares In Grid (5月27日)
开头 这是每周比赛中的第一道题,博主试了好几次坑后才勉强做对了,第二道题写的差不多结果去试时结果比赛已经已经结束了(尴尬),所以今天只记录第一道题吧 题目原文 Magic Squares In Gri ...
- 【Leetcode_easy】840. Magic Squares In Grid
problem 840. Magic Squares In Grid solution: class Solution { public: int numMagicSquaresInside(vect ...
- 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 ...
- 【LeetCode】840. Magic Squares In Grid 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 利用河图规律 暴力解法 日期 题目地址:https: ...
- 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 ...
随机推荐
- java hashmap的一些分析记录
最近朋友去面试被问了些hashmap相关的问题,hashmap的初始容量啊,什么操作最耗时等,之前看过hashmap的源码,正好这里也在总结下. 主要围绕下面几个点: HashMap是由数组+链表(j ...
- Django学习笔记(二)视图函数
一.url映射 1.为什么回去urls.py文件中找映射? 在‘settings.py’文件中配置了‘ROOT_URLCONF’为‘urls.py’.所有的django回去urls.py中寻找. 2. ...
- Django 连接mysql数据库中文乱码
Django 连接mysql数据库中文乱码 2018年08月25日 20:55:15 可乐乐乐乐乐 阅读数:566 版本:CentOS6.8 python3.6.4 django1.8.2 数据库 ...
- MySQL数据优化
很多企业,可能每天应对的数据量达百万,千万,甚至上亿的访问量,这样的量已经超过普通配置的mysql所承受的量,所以为了应对日益增长的访问量,我们需要对mysql做出相应的对策,进一步优化mysql以达 ...
- MQTT报文格式
MQTT报文结构 控制报文由三部分组成: 1.Fixed header 固定报头,所有报文都包含 2.Variable header 可变报头,部分报文包含 3.Body 有效载荷,部分报文包含 固定 ...
- 打造vim IDE
pathogen.vim:vim插件目录自动识别.加载(注意:能用pathogen.vim安装插件,就不要用Vundle.因为Vundle下载插件速度非常慢.) https://github.com/ ...
- Java 并查集Union Find
对于一组数据,主要支持两种动作: union isConnected public interface UF { int getSize(); boolean isConnected(int p,in ...
- C#学习-显式接口
显式的接口实现解决了命名冲突问题. 在使用显式的接口实现方式时,需要注意以下几个问题. 若显式实现接口,方法不能使用任何访问修饰符,显式实现的成员都默认为私有: 现式实现的成员默认是私有的,所以这些成 ...
- 关于pom.xml文件中引入net.sf.json-lib出错问题
关于pom.xml文件中引入net.sf.json-lib出错问题 在项目中引入以下依赖时一直报错 <dependency> <groupId>net.sf.json-lib& ...
- sql-我写的或者其他人写的
1.更新数据,补上分工 UPDATE smartoperator RIGHT JOIN smartoperation ON smartoperation.ID=smartoperator.Operat ...