题目链接:https://leetcode.com/problems/magic-squares-in-grid/description

attention:注意给定的数字不一定是1-9。

time:5ms

本人的解法过于粗糙,看出了中间必须是5,然后比较每行每列每对角线的值是否相等。

class Solution {
public:
int numMagicSquaresInside(vector<vector<int>>& grid) {
int n = grid.size();
int res = 0;
for(int i = 0; i < n - 2; i++){
for(int j = 0; j < n - 2; j++){
int sign = 0;
for(int k = i; k < i+3; k++){
for(int l = j; l < j+3; l++){
if(grid[k][l] > 9 || grid[k][l] < 1){
sign = 1;
break;
}
}
if(sign){
break;
}
}
if(sign){
continue;
}
if(grid[i+1][j+1] == 5){ if(grid[i][j]+grid[i+2][j+2]==grid[i+2][j]+grid[i][j+2]&&grid[i+2][j]+grid[i][j+2]==grid[i+1][j]+grid[i+1][j+2]&&grid[i+2][j]+grid[i][j+2]==grid[i][j+1]+grid[i+2][j+1]){ if(grid[i][j]+grid[i+1][j]+grid[i+2][j]==grid[i][j+2]+grid[i+1][j+2]+grid[i+2][j+2]&&grid[i][j]+grid[i+1][j]+grid[i+2][j]==15){
if(grid[i][j]+grid[i][j+1]+grid[i][j+2]==grid[i+2][j]+grid[i+2][j+1]+grid[i+2][j+2]&&grid[i][j]+grid[i][j+1]+grid[i][j+2]==15){
res++;
}
}
}
}
}
}
return res;
}
};

下面是看到的别人写的更简单的实现:

https://leetcode.com/problems/magic-squares-in-grid/discuss/133874/Python-5-and-43816729

840. Magic Squares In Grid ——weekly contest 86的更多相关文章

  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

    class Solution { public: int numMagicSquaresInside(vector<vector<int>>& grid) { ; in ...

  5. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

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

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

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

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

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

随机推荐

  1. 在KEIL下查看单片机编程内存使用情况

    原文链接:https://blog.csdn.net/D_azzle/article/details/83410141 截至到目前为止,本人接触单片机也有将近一年的时间.这一年以来也接触过了很具代表性 ...

  2. C 面向对象编程 --- 一模块的串口协议解析

    // 任务目的// 解析串口收到的54个字节.这54个字节包含了8个车道的5大信息以及校验信息.// 实现了查询每条车道包含了哪些信息. #include <stdio.h>#includ ...

  3. 大话Python类语义

    类 物以类聚,人以群分,就是相同特征的人和事物会自动聚集在一起,核心驱动点就是具有相同特征或相类似的特征,我们把具有相同特征或相似特征的事物放在一起,被称为分类,把分类依据的特征称为类属性 计算机中分 ...

  4. 基于raft共识搭建的Fabric1.4.4多机网络环境

    1准备工作介绍 1各个主机ip以及节点分配情况 各个主机的节点分配情况 ip地址 orderer0.example.com,peer0.org1.example.com 172.17.3.60 ord ...

  5. android的adb命令整理

    adb.exe的路径在Android\Sdk\platform-tools 把这个路径加入到系统的path环境下. 先用usb连接设备,比如一台android手机 adb tcpip 5555 adb ...

  6. Linux执行脚本让进程挂掉后自动重启

    1 创建循环监听脚本  autostart.sh 例: 其中futures-market-server-v3andwebsoket.jar 是要监听的执行程序 #/bin/bashwhile true ...

  7. IDEA设置maven修改settings.xml配置文件无法加载仓库

    作为初学者配置maven一般网上搜索.然后你就看到各种配置文件片段,首先配置镜像,然后配置仓库.完事后再IDEA里面配置下maven的路径和配置文件路径. 这些文章属实坑爹,完全没讲一个重要的配置就是 ...

  8. 2016年 实验三 B2C模拟实验

    实验三 B2C模拟实验 [实验目的] 掌握网上购物的基本流程和B2C平台的运营 [实验条件] ⑴.个人计算机一台 ⑵.计算机通过局域网形式接入互联网. (3).奥派电子商务应用软件 [知识准备] 本实 ...

  9. day22 函数整理

    # 1.计算 年月日时分秒 于现在之间差了多少 格式化时间 # 现在 # 某一个年月日时分秒 参数 # import time # def get_time(old_t,fmt = '%Y-%m-%d ...

  10. day20 Pyhton学习 面向对象-类与类之间的关系

    一.类与类之间的依赖关系 class Elphant: def __init__(self, name): self.name = name def open(self, ref): print(&q ...