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 解释: 下面的子矩阵是一个 3 x 3 的幻方: 438 951 276 而这一个不是: 384 519 762 总的来说,在本示例所给定的矩阵中只有一个 3 x 3 的幻方子矩阵。
提示:
- 1 <= grid.length = grid[0].length <= 10
- 0 <= grid[i][j] <= 15
注意题目中说的是1到9的数字
class Solution {
public:
int numMagicSquaresInside(vector<vector<int> >& grid) {
int res = 0;
int r = grid.size();
int c = grid[0].size();
for(int i = 0; i <= r - 3; i++)
{
for(int j = 0; j <= c - 3; j++)
{
bool check = true;
int flag = grid[i][j] + grid[i][j + 1] + grid[i][j + 2];
if( grid[i + 1][j] + grid[i + 1][j + 1] + grid[i + 1][j + 2] != flag
||grid[i + 2][j] + grid[i + 2][j + 1] + grid[i + 2][j + 2] != flag
||grid[i][j] + grid[i + 1][j + 1] + grid[i + 2][j + 2] != flag
||grid[i + 2][j] + grid[i + 1][j + 1] + grid[i][j + 2] != flag)
{
check = false;
}
for(int y = i; y <= i + 2; y++)
for(int x = j; x <= j + 2; x++)
if(grid[y][x] > 9 || grid[y][x] <= 0)
{
check = false;
break;
}
if(check)
res++;
}
}
return res;
}
};
Leetcode840.Magic Squares In Grid矩阵中的幻方的更多相关文章
- [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 ...
- 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 ...
- [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 ...
- C#LeetCode刷题之#840-矩阵中的幻方(Magic Squares In Grid)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3752 访问. 3 x 3 的幻方是一个填充有从 1 到 9 的不 ...
- 【LeetCode】840. Magic Squares In Grid 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 利用河图规律 暴力解法 日期 题目地址:https: ...
- LeetCode算法题-Magic Squares In Grid(Java实现)
这是悦乐书的第326次更新,第349篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第196题(顺位题号是840).3 x 3魔方是一个3 x 3网格,填充了从1到9的不同 ...
- 840. Magic Squares In Grid
class Solution { public: int numMagicSquaresInside(vector<vector<int>>& grid) { ; in ...
- 840. Magic Squares In Grid ——weekly contest 86
题目链接:https://leetcode.com/problems/magic-squares-in-grid/description attention:注意给定的数字不一定是1-9. time: ...
随机推荐
- 推荐大家一个个人觉得超级好的Java学习网站
https://how2j.cn?p=16567 这是网址,网站里有Java基础,Javaweb.框架.数据库.工具.面试题等等的,站长一直在更新新的知识,很好用哦
- python numpy.shape 和 numpy.reshape函数
导入numpy模块 from numpy import * import numpy as np ############################################### ...
- 基于PHP的一种Cache回调与自动触发技术
$s = microtime(true); for($i=0; $iaaa($array, $array, $array); $data = a::bbb($array, $array, $array ...
- Java数据结构和算法(八)--红黑树与2-3树
红黑树规则: 1.每个节点要么是红色,要么是黑色 2.根节点都是黑色节点 3.每个叶节点是黑色节点 3.每个红色节点的两个子节点都是黑色节点,反之,不做要求,换句话说就是不能有连续两个红色节点 4.从 ...
- 只要三步!阿里云DLA帮你处理海量JSON数据
概述 您可能有大量应用程序产生的JSON数据,您可能需要对这些JSON数据进行整理,去除不想要的字段,或者只保留想要的字段,或者仅仅是进行数据查询. 那么,利用阿里云Data Lake Analyti ...
- win8 装的SQL Server2012 企业版
链接:ed2k://|file|cn_sql_server_2012_enterprise_edition_x86_x64_dvd_813295.iso|5054384128|BC78EFDC4005 ...
- Spring事务传播行为详解
前言 Spring在TransactionDefinition接口中规定了7种类型的事务传播行为.事务传播行为是Spring框架独有的事务增强特性,他不属于的事务实际提供方数据库行为.这是Spring ...
- Angungular.js 的过滤器&工具方法
字母大小写 数字 货币 截取字符串 截取数组 用JS操作 ----------------------------------------------------------------------- ...
- day18 9.转账汇款案例(1)
- Redis 复制、Sentinel的搭建和原理说明(转)
Redis 复制.Sentinel的搭建和原理说明 转自:http://www.cnblogs.com/zhoujinyi/p/5570024.html. 背景: Redis-Sentinel是Re ...