作者: 负雪明烛
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:

  1. Input: [[4,3,8,4],
  2. [9,5,1,9],
  3. [2,7,6,2]]
  4. Output: 1
  5. Explanation:
  6. The following subgrid is a 3 x 3 magic square:
  7. 438
  8. 951
  9. 276
  10. while this one is not:
  11. 384
  12. 519
  13. 762
  14. 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个数相加都相等

  1. class Solution:
  2. def numMagicSquaresInside(self, grid):
  3. """
  4. :type grid: List[List[int]]
  5. :rtype: int
  6. """
  7. if len(grid) < 3 or len(grid[0]) < 3:
  8. return 0
  9. counter = 0
  10. for row in range(len(grid) - 2):
  11. for col in range(len(grid[0]) - 2):
  12. sub_matrix = [[grid[row + i][col + j] for j in range(3)] for i in range(3)]
  13. if self.magic_square(sub_matrix):
  14. counter += 1
  15. return counter
  16. def magic_square(self, matrix):
  17. is_number_right = all(1 <= matrix[i][j] <= 9 for i in range(3) for j in range(3))
  18. is_row_right = all(sum(row) == 15 for row in matrix)
  19. is_col_right = all(sum(col) == 15 for col in [[matrix[i][j] for i in range(3)] for j in range(3)])
  20. is_diagonal_right = matrix[1][1] == 5 and matrix[0][0] + matrix[-1][-1] == 10 and matrix[0][-1] + matrix[-1][0] == 10
  21. is_repeat_right = len(set(matrix[i][j] for i in range(3) for j in range(3))) == 9
  22. return is_number_right and is_row_right and is_col_right and is_diagonal_right and is_repeat_right

暴力解法

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

  1. class Solution(object):
  2. def numMagicSquaresInside(self, grid):
  3. """
  4. :type grid: List[List[int]]
  5. :rtype: int
  6. """
  7. M, N = len(grid), len(grid[0])
  8. res = 0
  9. for r in range(M - 2):
  10. for c in range(N - 2):
  11. curgrid = [[grid[r + i][c + j] for j in range(3)] for i in range(3)]
  12. if self.isMagic(curgrid):
  13. res += 1
  14. return res
  15. def isMagic(self, grid):
  16. count = list(range(9))
  17. for i in range(3):
  18. for j in range(3):
  19. if not (1 <= grid[i][j] <= 9):
  20. return False
  21. count[grid[i][j] - 1] += 1
  22. if 0 in count: return False
  23. row, col = [0, 0, 0], [0, 0, 0]
  24. for i in range(3):
  25. row[i] += sum(grid[i][j] for j in range(3))
  26. for j in range(3):
  27. col[j] += sum(grid[i][j] for i in range(3))
  28. if row[0] != row[1] != row[2] or col[0] != col[1] != col[2]:
  29. return False
  30. if grid[0][0] + grid[2][2] != grid[0][2] + grid[2][0]:
  31. return False
  32. 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. python-3.x- 序列操作

    1. list操作 A.添加元素 1 list = ["C++","C", "Java", "Python"] 2 &q ...

  2. 日常Java 2021/9/29

    StringBuffer方法 public StringBuffer append(String s) 将指定的字符串追加到此字符序列. public StringBuffer reverse() 将 ...

  3. 日常Java 2021/9/26 (二柱升级版)

    package m; import java.util.Scanner;import java.util.Random; public class di_er { static int number= ...

  4. 学习java 7.27

    学习内容: 创建树 Swing 使用JTree对象来代表一棵树,JTree树中结点可以使用TreePath来标识,该对象封装了当前结点及其所有的父结点. 当一个结点具有子结点时,该结点有两种状态: 展 ...

  5. Java偏向锁浅析

    偏向锁的定义 顾名思义,偏向锁会偏向第一个访问锁的线程. 如果在接下来的运行过程中,该锁没有被其他线程访问,这持有偏向锁的线程将永远不需要同步 如果在运行过程中,遇到了其他线程抢占锁,则持有偏向锁的线 ...

  6. 数据存储SharePreferences详解

    1.SharedPreferences存储 SharedPreferences时使用键值对的方式来存储数据的,也就是在保存一条数据时,需要给这条数据提供一个对应的键,这样在读取的时候就可以通过这个键把 ...

  7. MapStruct对象转换

    第一次看到 MapStruct 的时候, 我个人非常的开心.因为其跟我内心里面的想法不谋而合. 1 MapStruct 是什么? 1.1 JavaBean 的困扰 对于代码中 JavaBean之间的转 ...

  8. mysql 间隙锁专题

    本文研究记录mysql间隙锁,涉及以下情况 唯一索引 非唯一索引 范围更新 等值更新 mysql8 mysql7 RR RC 数据准备 mysql> select * from vodb.tes ...

  9. html框架frame iframe

    框架 通过使用框架,你可以在同一个浏览器窗口中显示不止一个页面.没分HTML文档称作一个框架. 缺点: 开发人员必须同时跟踪更多的HTML文档 很难打印整张页面 框架结构标签(<frameset ...

  10. Linux:$?,$n,$#,$0

    $? 获取执行上一个指令的返回值(0为成功,非零为失败) $n 获取当前执行的shell脚本的第n个参数值,n=1...9,当n=0的时表示脚本的文件名,如果n大于9,大括号括起来${10} $# 获 ...