leetcode Day28----array.easy

博客探讨网格中3x3幻方数量的计算问题,幻方要求3x3网格填入1到9的不同数字,使每行、列和对角线和相同。给出示例输入输出,还展示了Java和Python3的实现结果,包括运行时间和内存使用情况,博主表示后续优先用Java和C,再用Python3。

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, 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

JAVA

class Solution {
    public int numMagicSquaresInside(int[][] grid) {
        int m = grid.length, n = grid[0].length, result = 0;
        for (int i = 0; i < m - 2; i++) {
            for (int j = 0; j < n - 2; j++) {
                if (isMagic(grid, i, j)) {
                    result++;
                }
            }
        }
        return result;
    }
    
private boolean isMagic(int[][] grid, int row, int col) {
    int[] record = new int[10];
    for (int i = row; i < row + 3; i++) {
        for (int j = col; j < col + 3; j++) {
            if (grid[i][j] < 1 || grid[i][j] > 9 || record[grid[i][j]] > 0) {
                return false;
            }
            record[grid[i][j]] = 1;
        }
    }
    int sum1 = grid[row][col] + grid[row + 1][col + 1] + grid[row + 2][col + 2];
    int sum2 = grid[row][col + 2] + grid[row + 1][col + 1] + grid[row + 2][col];
    if (sum1 != sum2) {
        return false;
    }
    for (int i = 0; i < 3; i++) {
        if (grid[row + i][col] + grid[row + i][col + 1] + grid[row + i][col + 2] != sum1){
            return false;
        }
        if (grid[row][col + i] + grid[row + 1][col + i] + grid[row + 2][col + i] != sum1) {
            return false;
        }
    }
    return true;
}
}

Success
Details
Runtime: 0 ms, faster than 100.00% of Java online submissions for Magic Squares In Grid.
Memory Usage: 33.9 MB, less than 100.00% of Java online submissions for Magic Squares In Grid.

python3

class Solution:
    def numMagicSquaresInside(self, grid: List[List[int]]) -> int:
        ct = 0
        for i in range(0,len(grid)-2):
            for j in range(0,len(grid[i])-2):
                strs = grid[i][j:j+3] +  grid[i+1][j:j+3] + grid[i+2][j:j+3]
                t = set(strs)
                mx,mn = max(t),min(t)
                if(len(t)==9 and mx==9 and mn==1):
                    diag1 = grid[i+1][j+1] + grid[i][j] + grid[i+2][j+2]
                    diag2 = grid[i][j+2] + grid[i+1][j+1] + grid[i+2][j]
                    row1= sum(grid[i][j:j+3])
                    row2= sum(grid[i+1][j:j+3])
                    row3= sum(grid[i+2][j:j+3]) 
                    col1=grid[i][j]+grid[i+1][j]+grid[i+2][j]
                    col2=grid[i][j+1]+grid[i+1][j+1]+grid[i+2][j+1]
                    col3=grid[i][j+2]+grid[i+1][j+2]+grid[i+2][j+2]
                    if(diag1==diag2 and diag2==row1 and row1==row2 and row2==row3 and row3==col1 and col1==col2 and col2==col3):
                        ct+=1
        return ct

Success
Details
Runtime: 40 ms, faster than 72.62% of Python3 online submissions for Magic Squares In Grid.
Memory Usage: 13.4 MB, less than 7.69% of Python3 online submissions for Magic Squares In Grid.

博主认为python实在太开挂了。。以后改目标了,优先java和C,再做一个python3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值