840. Magic Squares In Grid
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的更多相关文章
- 【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: ...
- 840. Magic Squares In Grid ——weekly contest 86
题目链接:https://leetcode.com/problems/magic-squares-in-grid/description attention:注意给定的数字不一定是1-9. time: ...
- [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 ...
- [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_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 ...
- 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 的不 ...
随机推荐
- HDU-1459.非常可乐(BFS )
这道题TLE了很多次,原来一直以为将数字化为最简可以让运算更快,但是去了简化之后才发现,真正耗时的就是化简....还和队友学到了用状态少直接数组模拟刚就能过... 本题大意:给出可乐的体积v1,给出两 ...
- 【laravel VS lumen】
读取项目的配置信息 读取config文件database.php中的default属性信息 laravel:config('database.default'); lumen:app()->co ...
- POJ 1177 Picture(线段树周长并)
描述 A number of rectangular posters, photographs and other pictures of the same shape are pasted on ...
- [剑指Offer]45-把数组排成最小的数
题目链接 https://www.nowcoder.com/practice/8fecd3f8ba334add803bf2a06af1b993?tpId=13&tqId=11185&t ...
- C#字符串长度判断
string aaa = "你好123"; Label1.Text = aaa.Length.ToString(); //结果5 Label2.Text = System.Tex ...
- AngularJS——第8章 服务
第8章 服务 服务是一个对象或函数,对外提供特定的功能. 8.1 内建服务 1. $location是对原生Javascript中location对象属性和方法的封装. // $location内置服 ...
- error:cv_bridge---opencv和ros连接起来的桥
有多个warning出现...版本冲突 解决办法: cv_bridge[opencv和ros连接起来的桥],默认依赖的opencv版本是2.4.8,如果安装了新的opencv版本,比如3.1.0,那么 ...
- stark组件开发之URL分发和默认Handler
为register 函数添加一个,prev参数,默认None ,用于可以让用户自己指定前缀. def register(self, model_class, handler_class=None, p ...
- c# 24种设计模式4建造者模式(Builder)
先来一个例子 建造者接口 public interface IBuilder { void CreateLogo(); void CreateBody(); void CreateWheel(); v ...
- (转)Android学习路线总结,绝对干货
一.前言 不知不觉自己已经做了几年开发了,由记得刚出来工作的时候感觉自己能牛逼,现在回想起来感觉好无知.懂的越多的时候你才会发现懂的越少. 如果你的知识是一个圆,当你的圆越大时,圆外面的世界也就越大. ...