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 的不 ...
随机推荐
- 189. Rotate Array(Array)
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- mysql 数据库名称,中间带有中划线问题
插入数据时候,引用了数据库名,数据库名中有横线,会提示错误: You have an error in your SQL syntax; check the manual that correspon ...
- cf相关命令
进行登录的命令: cf login -a https://api.bupaas.citicsinfo.com --skip-ssl-validation 进行发布的命令: cf push gwdemo ...
- Composer 安装以及使用方法
Composer 是 PHP 的一个依赖管理工具.它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们. Linux 下安装 curl -sS https://getcomposer.org/ ...
- golang语言中os包的学习与使用(文件,目录,进程的操作)
os中一些常用函数的使用: package main; import ( "os" "fmt" "time" "strings&q ...
- win下apache的error.log和access.log文件过大
在httpd.conf中修改ErrorLog和CustomLog的配置 ErrorLog "|E:/apache2.2/bin/rotatelogs.exe E:/apache2.2/log ...
- Vue filter-v-for 使用
var app5 = new Vue({ el: '#app5', data: { shoppingList: [ "Milk", "Donuts", &quo ...
- 【Linux】关于路由跟踪指令traceroute
稍有计算机常识的人都知道ping命令,是用来检查自己的主机是否与目标地址接通,自己的主机与目标地址的通讯包通讯速率,所谓的通讯包也就是那些什么TCP/IP,UDP包,这里说得通俗一点,比如,就拿这 ...
- (转)关于X64位系统IIS7下支持32位asp.net程序
最近在windows2008 x64位系统下的IIS7下部署asp.net程序. vs2005或vs2008默认的情况下是Any cpu 的也就是支持x86和x64两种系统的.可我的程序在引用了一个三 ...
- javascript 高级程序设计 三
Sorry,前两张介绍的主题还是JavaScript,而第一章介绍了JavaScript和ECMAScript区别,所以前两章介绍的主题应该改为ECMAScript,但是 标题就不改了因为现在人们习惯 ...