840. Magic Squares In Grid Medium

@problem@discussion
#Array#Math#Matrix



1/**
2 * [840] Magic Squares In Grid
3 *
4 * 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.
5 * Given a row x col grid of integers, how many 3 x 3 "magic square" subgrids are there?  (Each subgrid is contiguous).
6 *  
7 * Example 1:
8 * <img alt="" src="https://assets.leetcode.com/uploads/2020/09/11/magic_main.jpg" style="width: 322px; height: 242px;" />
9 * Input: grid = [[4,3,8,4],[9,5,1,9],[2,7,6,2]]
10 * Output: 1
11 * Explanation: 
12 * The following subgrid is a 3 x 3 magic square:
13 * <img alt="" src="https://assets.leetcode.com/uploads/2020/09/11/magic_valid.jpg" style="width: 242px; height: 242px;" />
14 * while this one is not:
15 * <img alt="" src="https://assets.leetcode.com/uploads/2020/09/11/magic_invalid.jpg" style="width: 242px; height: 242px;" />
16 * In total, there is only one magic square inside the given grid.
17 * 
18 * Example 2:
19 * 
20 * Input: grid = [[8]]
21 * Output: 0
22 * 
23 *  
24 * Constraints:
25 * 
26 * 	row == grid.length
27 * 	col == grid[i].length
28 * 	1 <= row, col <= 10
29 * 	0 <= grid[i][j] <= 15
30 * 
31 */
32pub struct Solution {}
33
34// problem: https://leetcode.com/problems/magic-squares-in-grid/
35// discuss: https://leetcode.com/problems/magic-squares-in-grid/discuss/?currentPage=1&orderBy=most_votes&query=
36
37// submission codes start here
38
39impl Solution {
40    pub fn num_magic_squares_inside(grid: Vec<Vec<i32>>) -> i32 {
41        0
42    }
43}
44
45// submission codes end
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_840() {
53    }
54}
55


Back
© 2025 bowen.ge All Rights Reserved.