作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/magic-squares-in-grid/description/

题目描述

A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, column, and both diagonals all have the same sum.

Given an grid of integers, how many 3 x 3 “magic square” subgrids are there? (Each subgrid is contiguous).

Example 1:

Input: [[4,3,8,4],
[9,5,1,9],
[2,7,6,2]]
Output: 1 Explanation: The following subgrid is a 3 x 3 magic square:
438
951
276 while this one is not:
384
519
762 In total, there is only one magic square inside the given grid.

Note:

  • 1 <= grid.length <= 10
  • 1 <= grid[0].length <= 10
  • 0 <= grid[i][j] <= 15

题目大意

判断一个大矩阵中有多少河图。河图这个词很古典文化,其实就是1到9填在9个格子中,让横竖斜的3个数相加都相等。

解题方法

利用河图规律

直接按照河图的规定去做就OK了。用到了一个结论:河图的中心数字是5.

注意一个易忽略的点,就是所有的数字应该在1~9之间。测试用例里面出现了不在这个范围内的数字也能组成河图。

另外,关于河图,其实有很多有用的结论,我并没有使用。

河图记忆方法:偶角奇边坐心五.一线双角相对画.

这个帖子挺有意思的:1到9填在9个格子中,让横竖斜的3个数相加都相等

class Solution:
def numMagicSquaresInside(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
if len(grid) < 3 or len(grid[0]) < 3:
return 0
counter = 0
for row in range(len(grid) - 2):
for col in range(len(grid[0]) - 2):
sub_matrix = [[grid[row + i][col + j] for j in range(3)] for i in range(3)]
if self.magic_square(sub_matrix):
counter += 1
return counter def magic_square(self, matrix):
is_number_right = all(1 <= matrix[i][j] <= 9 for i in range(3) for j in range(3))
is_row_right = all(sum(row) == 15 for row in matrix)
is_col_right = all(sum(col) == 15 for col in [[matrix[i][j] for i in range(3)] for j in range(3)])
is_diagonal_right = matrix[1][1] == 5 and matrix[0][0] + matrix[-1][-1] == 10 and matrix[0][-1] + matrix[-1][0] == 10
is_repeat_right = len(set(matrix[i][j] for i in range(3) for j in range(3))) == 9
return is_number_right and is_row_right and is_col_right and is_diagonal_right and is_repeat_right

暴力解法

二刷的时候完全忘了什么性质了……直接暴力解法,这样的话,需要按照题目做各种判断,所以函数更为复杂了。

class Solution(object):
def numMagicSquaresInside(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
M, N = len(grid), len(grid[0])
res = 0
for r in range(M - 2):
for c in range(N - 2):
curgrid = [[grid[r + i][c + j] for j in range(3)] for i in range(3)]
if self.isMagic(curgrid):
res += 1
return res def isMagic(self, grid):
count = list(range(9))
for i in range(3):
for j in range(3):
if not (1 <= grid[i][j] <= 9):
return False
count[grid[i][j] - 1] += 1
if 0 in count: return False
row, col = [0, 0, 0], [0, 0, 0]
for i in range(3):
row[i] += sum(grid[i][j] for j in range(3))
for j in range(3):
col[j] += sum(grid[i][j] for i in range(3))
if row[0] != row[1] != row[2] or col[0] != col[1] != col[2]:
return False
if grid[0][0] + grid[2][2] != grid[0][2] + grid[2][0]:
return False
return True

日期

2018 年 5 月 27 日 —— 周末的天气很好~
2018 年 11 月 24 日 —— 周六快乐

【LeetCode】840. Magic Squares In Grid 解题报告(Python)的更多相关文章

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

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

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

  5. 840. Magic Squares In Grid

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

  6. 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...

  7. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  8. 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...

  9. 【LeetCode】764. Largest Plus Sign 解题报告(Python)

    [LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

随机推荐

  1. jmeter非GUI(cmd命令行)模式的压测和输出测试报告

    1.非GUI模式的压测,和GUI有啥不同? 2.非GUI模式怎么搞? 大家打开jmeter的时候,都会看到这个界面: 注意看这句话: Don't use GUI mode for load testi ...

  2. .NET SAAS 架构与设计 -SqlSugar ORM

    1.数据库设计 常用的Saas分库分为2种类型的库 1.1 基础信息库 主要存组织架构 .权限.字典.用户等 公共信息 性能优化:因为基础信息库是共享的,所以我们可以使用 读写分离,或者二级缓存来进行 ...

  3. LeetCode替换空格

    LeetCode 替换空格 题目描述 请实现一个函数,把字符串 s 中的每个空格替换成"%20". 实例 1: 输入:s = "We are happy." 输 ...

  4. javascript的事件循环机制

    JavaScript是一门编程语言,既然是编程语言那么就会有执行时的逻辑先后顺序,那么对于JavaScript来说这额顺序是怎样的呢? 首先我们我们需要明确一点,JavaScript是单线程语言.所谓 ...

  5. Java【常用的日期操作】

    目录 1.设置时间 2.获取年月日时分秒 3.通过运算获取时间 4.和Date类转换 5.格式化时间 6.新功能LocalDate:当前日期格式化 7.示例 java.util.Calendar 类是 ...

  6. @RequestBody配合@Valid 校验入参参数

    自定义一个Controller import com.example.demo.pojo.User; import org.springframework.web.bind.annotation.Po ...

  7. spring注解-扩展原理

    AnnotationConfigApplicationContext(IOC容器)的有参构造方法中,在refresh()里对这些组件进行初始化 BeanPostProcessor bean后置处理器, ...

  8. Java实现邮件收发

    一. 准备工作 1. 传输协议 SMTP协议-->发送邮件: 我们通常把处理用户smtp请求(邮件发送请求)的服务器称之为SMTP服务器(邮件发送服务器) POP3协议-->接收邮件: 我 ...

  9. 看看线程特有对象ThreadLocal

    作用:设计线程安全的一种技术. 在使用多线程的时候,如果多个线程要共享一个非线程安全的对象,常用的手段是借助锁来实现线程的安全.线程安全隐患的前提是多线程共享一个不安全的对象 ,那么有没有办法让线程之 ...

  10. ActiveMQ(二)——ActiveMQ的安装和基本使用

    一:安装 2.启动之后成功 二.创建实例测试ActiveMQ 配置Maven所需的依赖 <dependency> <groupId>org.apache.activemq< ...