【Leetcode_easy】840. Magic Squares In Grid
problem
solution:
class Solution {
public:
int numMagicSquaresInside(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[].size();
int cnt = ;
for(int i=; i<m-; ++i)
{
for(int j=; j<n-; ++j)
{
if(isvalid(grid, i, j)) cnt++;
}
}
return cnt;
}
bool isvalid(vector<vector<int>>& grid, int i, int j) {
vector<int> cnt();
for(int x=i-; x<=i+; x++)//for loop errr...
{
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]!=) 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;
if(grid[i-][j+]+grid[i][j]+grid[i+][j-] != ) return false;
return true;
}
};
参考
1. Leetcode_easy_840. Magic Squares In Grid;
2. grandyang;
完
【Leetcode_easy】840. Magic Squares In Grid的更多相关文章
- 【LeetCode】840. Magic Squares In Grid 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 利用河图规律 暴力解法 日期 题目地址:https: ...
- 840. Magic Squares In Grid (5月27日)
开头 这是每周比赛中的第一道题,博主试了好几次坑后才勉强做对了,第二道题写的差不多结果去试时结果比赛已经已经结束了(尴尬),所以今天只记录第一道题吧 题目原文 Magic Squares In Gri ...
- 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: ...
- 【RAC】 RAC For W2K8R2 安装--grid的安装(四)
[RAC] RAC For W2K8R2 安装--grid的安装(四) 一.1 BLOG文档结构图 一.2 前言部分 一.2.1 导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学 ...
- LeetCode算法题-Magic Squares In Grid(Java实现)
这是悦乐书的第326次更新,第349篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第196题(顺位题号是840).3 x 3魔方是一个3 x 3网格,填充了从1到9的不同 ...
- 【Leetcode_easy】977. Squares of a Sorted Array
problem 977. Squares of a Sorted Array solution: class Solution { public: vector<int> sortedSq ...
- C#LeetCode刷题之#840-矩阵中的幻方(Magic Squares In Grid)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3752 访问. 3 x 3 的幻方是一个填充有从 1 到 9 的不 ...
- [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 ...
随机推荐
- [React] Create an Animate Content Placeholder for Loading State in React
We will create animated Content Placeholder as React component just like Facebook has when you load ...
- cocoapods安装错误的原因
gem 可以理解为管理RUBY库和程序包的查找,安装,升级和卸载是个非常好用的工具. gem install cocoapods过程中出现错误的问题.1.gem的源设置错误应该参照,下面来执行gem ...
- js原型和原型链的问题
<script> //js原型和原型链的概念 functionperson(name){ this.name=name; } person.prototype.age=18; person ...
- 010——MATLAB运行错误跳到下一个循环
(一)MATLAB运行错误跳到下一个循环 :%文件的个数 try %运行的程序放到这里 catch continue%假如上面的没法执行则执行continue,到下个循环 end
- 结构化异常SEH处理机制详细介绍(二)
本文将全面阐述__try,__except,__finally,__leave异常模型机制,它也即是Windows系列操作系统平台上提供的SEH模型.SEH实际包含两个主要功能:结束处理(termin ...
- 【转】根据Quartz-Cron表达式获取最近几次执行时间
public static List<String> getRecentTriggerTime(String cron) { List<String> list = new A ...
- P5590 【赛车游戏】
果然我还是太\(Naive\)了 首先有一些点/边其实是没有意义的,如果从1出发不能到该点或者从该点不能到n,这个点就可以不用管了.这个过程可以用正反两边\(dfs/bfs\)实现 然后删掉那些点之后 ...
- Day15:大前端
垂直水平居中 css: display: table-cell; text-align: center; vertical-align: middle; div: display: inline-bl ...
- CSS3字体大小rem属性用法
PX为单位 在Web页面初期制作中,我们都是使用“px”来设置我们的文本,因为他比较精确和固定. 只要页面某元素设置了px字体大小,其子元素/子孙元素未设置字体大小或设置的字体大小css优先级没父元素 ...
- Python自动生成代码工具
项目中有一个需求,对一个基类而言,拥有一个比较方法和拷贝方法,某些地方需要频繁地对这两个方法进行调用.对于所有子类而言,需要重写这两个方法,并在其中维护类内一些成员变量.例如有一个变量m_iMyVal ...