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. Ant 使用指南 与 知识汇总

    一.Ant是什么?        Ant是一种基于Java和XML的build工具.它可以帮助我们将项目开发过程中需要完成的各种步骤组织起来,通过一个简易的方式来构建整个项目.Ant究竟能做什么呢?这 ...

  2. map按照value值排序

    map可以实现key到value的一一映射,如果是一对多的,我们可以使用multimap multimap<int,int>mp; mp.insert(make_pair(first,se ...

  3. Beginning C# Programming with Unity

    Welcome to the wonderful world of programming! In this book you’ll learn the basics of programming u ...

  4. Linux 下 mysql的基本配置

    Linux 下 mysql的基本配置 2013年02月27日 ⁄ MySQL ⁄ 共 3000字 ⁄ 暂无评论 ⁄ 被围观 2,483 views+ 1. Linux mysql安装:    $ yu ...

  5. css3动画:执行前不显示,执行后显示

    今天做一个轮播,轮播图上使用animation.需求是当滚动到当前图片时,图片中的div从无到有逐渐显示, 遇到的问题是:页面刷新时,此图片上的div可以从无到有,但第二次滚动到此图片时,总是先显示一 ...

  6. js常用的数组,,字符串,,Math..正则方法

    数组 slice[start,end] 返回从原数组中指定开始下标到结束下标之间的项目组成新数组(不会影响原数组) splice() 1.删除功能:2个参数 , 起始位置 , 删除的项目 2.插入功能 ...

  7. __block的初步用法

    再block中使用 self 时,要在前面加上__block. 防止在block中用到self时把self对象retain, 造成内存泄露. __block UIViewController *saf ...

  8. win下apache的error.log和access.log文件过大

    在httpd.conf中修改ErrorLog和CustomLog的配置 ErrorLog "|E:/apache2.2/bin/rotatelogs.exe E:/apache2.2/log ...

  9. 更换Grade源为阿里云解决下载慢问题

    作为程序员,即写的了代码也得翻得上围墙. 现在包管理工具(Maven/grade)的便捷性让人大快朵颐,再也不需要到处找jar包. 如果你没梯子又苦苦无法加快下载速度,不如按照以下设置一下. 对单个项 ...

  10. IE7下面踩得坑

    bug1.position:fixed:z-index:99; 出现了z-index:2的层级跑到他上面了, 为什么?会出现这问题??? 检查: 1你的固定定位的容器是不是被其他容器包裹,你包裹得容器 ...