Class Solution

java.lang.Object
g0801_0900.s0840_magic_squares_in_grid.Solution

public class Solution extends Object
840 - Magic Squares In Grid.<p>Medium</p> <p>A <code>3 x 3</code> magic square is a <code>3 x 3</code> grid filled with distinct numbers <strong>from</strong> <code>1</code> <strong>to</strong> <code>9</code> such that each row, column, and both diagonals all have the same sum.</p> <p>Given a <code>row x col</code> <code>grid</code> of integers, how many <code>3 x 3</code> &ldquo;magic square&rdquo; subgrids are there? (Each subgrid is contiguous).</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2020/09/11/magic_main.jpg" alt="" /></p> <p><strong>Input:</strong> grid = [[4,3,8,4],[9,5,1,9],[2,7,6,2]]</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> The following subgrid is a 3 x 3 magic square: <img src="https://assets.leetcode.com/uploads/2020/09/11/magic_valid.jpg" alt="" /> while this one is not: <img src="https://assets.leetcode.com/uploads/2020/09/11/magic_invalid.jpg" alt="" /> In total, there is only one magic square inside the given grid.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> grid = [[8]]</p> <p><strong>Output:</strong> 0</p> <p><strong>Constraints:</strong></p> <ul> <li><code>row == grid.length</code></li> <li><code>col == grid[i].length</code></li> <li><code>1 <= row, col <= 10</code></li> <li><code>0 <= grid[i][j] <= 15</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • numMagicSquaresInside

      public int numMagicSquaresInside(int[][] grid)