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的更多相关文章

  1. 【Leetcode_easy】840. Magic Squares In Grid

    problem 840. Magic Squares In Grid solution: class Solution { public: int numMagicSquaresInside(vect ...

  2. 840. Magic Squares In Grid (5月27日)

    开头 这是每周比赛中的第一道题,博主试了好几次坑后才勉强做对了,第二道题写的差不多结果去试时结果比赛已经已经结束了(尴尬),所以今天只记录第一道题吧 题目原文 Magic Squares In Gri ...

  3. 【LeetCode】840. Magic Squares In Grid 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 利用河图规律 暴力解法 日期 题目地址:https: ...

  4. 840. Magic Squares In Grid ——weekly contest 86

    题目链接:https://leetcode.com/problems/magic-squares-in-grid/description attention:注意给定的数字不一定是1-9. time: ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. LeetCode算法题-Magic Squares In Grid(Java实现)

    这是悦乐书的第326次更新,第349篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第196题(顺位题号是840).3 x 3魔方是一个3 x 3网格,填充了从1到9的不同 ...

  9. C#LeetCode刷题之#840-矩阵中的幻方(Magic Squares In Grid)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3752 访问. 3 x 3 的幻方是一个填充有从 1 到 9 的不 ...

随机推荐

  1. .py文件右键添加Edit with IDLE

    1.打开注册表(regedit) 2.找到这个目录:HKEY_CLASSES_ROOT\SystemFileAssociations 3.找到.py的项,逐层新建 4.shell和edit,默认值改为 ...

  2. Unity性能优化 – 脚本篇

    https://wuzhiwei.net/unity_script_optimization/

  3. Codeforces Beta Round #65 (Div. 2)

    Codeforces Beta Round #65 (Div. 2) http://codeforces.com/contest/71 A #include<bits/stdc++.h> ...

  4. [剑指Offer]45-把数组排成最小的数

    题目链接 https://www.nowcoder.com/practice/8fecd3f8ba334add803bf2a06af1b993?tpId=13&tqId=11185&t ...

  5. Delphi: TMethod转普通方法为对象方法

    回看旧时代码,以往清楚之记忆渐渐模糊,事隔多年,遗忘正在发生. TMethod方法,当初熟练再看已陌生,再做复习以作备忘. 简言之,它做为一相桥接,将普通事件或方法转换为类方法,其多用于事件动态设置中 ...

  6. IIS下载,WebClient().DownloadFile下载

    new System.Net.WebClient().DownloadFile(serverPath, localPath); 有时候使用的时候,文件下载不下来.需要设置一下服务器上IIS的权限

  7. linux命令学习之:touch

    touch命令有两个功能:一是用于把已存在文件的时间标签更新为系统当前的时间(默认方式),它们的数据将原封不动地保留下来:二是用来创建新的空文件. 语法 touch(选项)(参数) 选项 -a:或-- ...

  8. [Mysql]——通过例子理解事务的4种隔离级别(转)

    第1级别:Read Uncommitted(读取未提交内容) 第2级别:Read Committed(读取提交内容) 第3级别:Repeatable Read(可重读) 第4级别:Serializab ...

  9. eclipse 安装python后pydev不出现

    一.环境 windows 7 64bit eclipse 4.5.2 pydev jdk7u55 二.安装步骤 1. 安装JDK eclipse依赖于java环境,所以需要安装java运行环境JRE. ...

  10. JFinal Web开发学习(一)开启HelloWorld

    初次接触JFinal框架,接触过MVC思想,接触过Spring. JFinal官网: http://www.jfinal.com/ 之前很嫌弃JavaWeb开发,主要原因是繁琐的xml配置. 官方推荐 ...