【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 ...
随机推荐
- Go奇技淫巧
判断io读取是否结束,尽量用if n==0这种方式,因为可以判断很多种情况 package main import ( "fmt" "io" "net ...
- 【DP入门到入土】
DP例题较多,可以根据自己需求食用~ update:下翻有状压DP入门讲解,也只有讲解了(逃~ DP的实质,就是状态的枚举. 一般用DP解决的问题,都是求计数或最优问题,所以这类问题,我们也可以用搜索 ...
- learning java Calendar类
//Calendar.MONTH ,这是一个特殊于日历的值. //在格里高利历和罗马儒略历中一年中的第一个月是 JANUARY,它为 0:最后一个月取决于一年中的月份数. // //所以这个值的初始值 ...
- Cogs 56. 质数取石子(博弈)
质数取石子 ★★ 输入文件:stonegame.in 输出文件:stonegame.out 简单对比 时间限制:1 s 内存限制:128 MB 问题描述 DD 和 MM 正在玩取石子游戏.他们的游戏规 ...
- 《挑战30天C++入门极限》C++运算符重载转换运算符
C++运算符重载转换运算符 为什么需要转换运算符? 大家知道对于内置类型的数据我们可以通过强制转换符的使用来转换数据,例如(int)2.1f;自定义类也是类型,那么自定义类的对象在很多情况下也 ...
- socket.error: [Errno 9] Bad file descriptor
这个错误很明显 ,是因为你关闭了套接字对象后,又再次去调用了套接字对象,此时套接字链接已经被关闭,你不能再去调用,所以才会出现这种错误,复查一下自己的代码,很快就可以解决. 参考: https://b ...
- seajs.config的解释
alias 别名配置,配置之后可在模块中使用require调用 require(‘jquery’); seajs.config({ alias: { 'jquery': 'jquery/jquery/ ...
- 像母语者一样说美语 How to Improve Spoken American English - Sound like a Native Speaker
视频讲解: 视频详情见:https://www.bilibili.com/video/av75075387/ 总结分析: 001 要点总结: 1. 本富兰克林方法: 要你写下一切听到的东西 2. 辅音 ...
- Out of range value for column 'huid' at row
遇到一个MySQL小问题 Data truncation: Out of range value for column 'huid' at row 1 在数据库某表中字段 “huid” 为 ...
- DNGuard HVM Unpacker(3.71 trial support and x64 fixed)
DNGuard HVM Unpacker(3.71 trial support and x64 fixed) Gr8 news. Finally got the x64 crash fixed. DN ...