【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 ...
随机推荐
- ssh远程后台运行
ssh hadoop8 "/export/server/storm/bin/storm nimbus >/export/server/storm/nimbus_start.log 2& ...
- mysqldump 原理(转载)
mysqldump 备份过程可以描述为: (1) 先发出一条 flush tables 关闭实例上所有打开的表(2) 创建一个全局锁,FLUSH TABLES WITH READ LOCK获得 db ...
- TCPDUMP抓包学习
一.抓包基础 1.抓网卡ens33 的包,有多大抓多大,然后保存到a.cap中 [root@localhost ~]# tcpdump -i ens33 -s -w a.cap tcpdump: li ...
- C++函数调用方式约定stdcall,cdecl,pascal,naked,thiscall,fastcall
https://www.cnblogs.com/xiangtingshen/p/11014514.html C++函数调用约定_cdecl约定:参数:从右向左依次入栈堆栈平衡:调用方平衡 #inclu ...
- mysql 函数表
Name Description ABS() Return the absolute value ACOS() Return the arc cosine ADDDATE() Add time val ...
- Vic-软件测试-开始软件测试
前言 大家好,我是 Vic,今天给大家带来开始软件测试的概述,希望你们喜欢 软件测试 软件测试的基本概念.方法.常用测试工具的使用 常用测试工具的使用性能自动化测试工具:jmeter.loadrunn ...
- js将数组分割成等长数组
方法一: function group(array, subGroupLength) { let index = 0; let newArray = []; while(index < arra ...
- SpringData 简单的条件查询
今天在写springdata条件查询时,JpaRepository的findOne方法,不知道是因为版本的原因还是其他原因,总是查询不出来数据 //springdata jpa版本为1.5.15,配置 ...
- JVM命令行参数
root@ubuntu-blade2:/sdf/jdk# javaUsage: java [-options] class [args...] (to execute a class) or java ...
- JDBC 操作
简单的 JDBC 操作主要有: JdbcTemplate query queryForObject queryForList update execute 简单使用如下所示. 初始化数据库 sprin ...