class Solution
{
public:
int numMagicSquaresInside(vector<vector<int>>& grid)
{ int count=;
int szx=grid.size();
int szy=grid[].size();
for(int i=;i<szx-;i++)
{
for(int j=;j<szy-;j++)
{
if(grid[i+][j+]==&&judge(grid,i,j))
count++;
}
}
return count;
} bool judge(vector<vector<int>> &grid,int i,int j)
{
if(grid[i][j]==) //check there are duplicates or not
return false;
for(int x=i;x<i+;x++) //judge cur num biggerthan 9 or not,if yes,return false
{
for(int y=j;y<j+;y++)
{
int curele=grid[x][y];
if(curele>)
return false;
}
}
for(int x=i;x<i+;x++) //judge every row's sum is 15 or not
{
if(grid[x][j]+grid[x][j+]+grid[x][j+]!=)
return false;
}
for(int y=j;y<j+;y++) //judge every col's sum is 15 or not
{
if(grid[i][y]+grid[i+][y]+grid[i+][y]!=)
return false;
}
if(grid[i][j]+grid[i+][j+]+grid[i+][j+]!=) //judge diagonals
return false;
if(grid[i+][j]+grid[i+][j+]+grid[i][j+]!=)
return false;
return true;
}
};

基本上是暴力解法,问题不大

840. Magic Squares In Grid的更多相关文章

  1. 【Leetcode_easy】840. Magic Squares In Grid

    problem 840. Magic Squares In Grid solution: class Solution { public: int numMagicSquaresInside(vect ...

  2. 840. Magic Squares In Grid (5月27日)

    开头 这是每周比赛中的第一道题,博主试了好几次坑后才勉强做对了,第二道题写的差不多结果去试时结果比赛已经已经结束了(尴尬),所以今天只记录第一道题吧 题目原文 Magic Squares In Gri ...

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

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 利用河图规律 暴力解法 日期 题目地址:https: ...

  4. 840. Magic Squares In Grid ——weekly contest 86

    题目链接:https://leetcode.com/problems/magic-squares-in-grid/description attention:注意给定的数字不一定是1-9. time: ...

  5. [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 ...

  6. [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 ...

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

  8. LeetCode算法题-Magic Squares In Grid(Java实现)

    这是悦乐书的第326次更新,第349篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第196题(顺位题号是840).3 x 3魔方是一个3 x 3网格,填充了从1到9的不同 ...

  9. C#LeetCode刷题之#840-矩阵中的幻方(Magic Squares In Grid)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3752 访问. 3 x 3 的幻方是一个填充有从 1 到 9 的不 ...

随机推荐

  1. 22. Generate Parentheses (backTracking)

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  2. mybatis知识点(已掌握)

    1.${} 和 #{} 的区别? ${} 直接显示传入数据,不能防止sql注入,一般用于传数据库对象(比如表名). #{} 传入数据被当成字符串,自动加上双引号,防止sql注入. 2.有哪些Execu ...

  3. TZOJ 4325 RMQ with Shifts(线段树查询最小,暴力更新)

    描述 In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each que ...

  4. Django1.0和2.0中的rest_framework的序列化组件之超链接字段的处理

    大家看到这个标题是不是有点懵逼,其实我就是想要一个这样的效果 比如我get一条书籍的数据,在一对多的字段中我们显示一个url,看起来是不是很绚! 下面我们就来实现这么一个东西 首先我们一对多字段中的一 ...

  5. [leetcode]270. Closest Binary Search Tree Value二叉搜索树中找target的最接近值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  6. RESTful接口规范

    一. 什么是RESTful REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移” REST从资源的角 ...

  7. ORACLE数据库的关闭与重启

    一.关闭数据库 1.SHUTDOWN IMMEDIATE 这是我们常用的一种关闭数据库的方式,想很快地关闭数据库,但又想让数据库干净的关闭,常采用这种方式. 当前正在被Oracle处理的SQL语句立即 ...

  8. mysql数据库存储经度纬度

    使用float或者double会自动四舍五入,用decimal(20,17)当然你用varchar也是可以的

  9. 集合 day8

    一,集合. 集合是无序的,不重复的数据集合,它里面的元素是可哈希的(不可变类型),但是集合本身是不可哈希(所以集合做不了字典的键)的.以下是集合最重要的两点: 去重,把一个列表变成集合,就自动去重了. ...

  10. Employee类

    package demo; import java.time.LocalDate; public class Employee { private String name; private doubl ...