package leetcode;

/**
 * https://leetcode.com/problems/magic-squares-in-grid/
 */
public class Problem840 {
    public int numMagicSquaresInside(int[][] grid) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem840 prob = new Problem840();
    }
}

@@ -167,128 +167,1971 @@
     
-// TODO%0A        return 0;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem840 prob = new Problem840()
+int maxRow = grid.length;%0A        int maxCol = 0;%0A        if (grid.length %3E 0) %7B%0A            maxCol = grid%5B0%5D.length;%0A        %7D%0A        int count = 0;%0A        for (int i = 0; i %3C grid.length; i++) %7B%0A            for (int j = 0; j %3C grid%5Bi%5D.length; j++) %7B%0A                if (isMagicSquare(grid, maxRow, maxCol, i, j)) %7B%0A                    count++;%0A                %7D%0A            %7D%0A        %7D%0A        return count;%0A    %7D%0A%0A    private static boolean isMagicSquare(int%5B%5D%5B%5D grid, int maxRow, int maxCol, int row, int col) %7B%0A        if (row + 2 %3E= maxRow %7C%7C col + 2 %3E= maxCol) %7B%0A            return false;%0A        %7D%0A        boolean%5B%5D found = new boolean%5B10%5D;%0A        // Check all rows and columns.%0A        int sumRow = 0;%0A        int sumCol = 0;%0A        for (int r1 = row, c2 = col; r1 %3C row + 3; r1++, c2++) %7B%0A            int tmpRow = 0;%0A            int tmpCol = 0;%0A            for (int c1 = col, r2 = row; c1 %3C col + 3; c1++, r2++) %7B%0A                int rowVal = grid%5Br1%5D%5Bc1%5D;%0A                int colVal = grid%5Br2%5D%5Bc2%5D;%0A                if (rowVal %3C 1 %7C%7C rowVal %3E 9 %7C%7C found%5BrowVal%5D) %7B%0A                    return false;%0A                %7D%0A                found%5BrowVal%5D = true;%0A                tmpRow += rowVal;%0A                tmpCol += colVal;%0A            %7D%0A            if (sumRow == 0) %7B%0A                sumRow = tmpRow;%0A            %7D else %7B%0A                if (sumRow != tmpRow) %7B%0A                    return false;%0A                %7D%0A            %7D%0A            if (sumCol == 0) %7B%0A                sumCol = tmpCol;%0A            %7D else %7B%0A                if (sumCol != tmpCol) %7B%0A                    return false;%0A                %7D%0A            %7D%0A        %7D%0A        // Check all diagonals.%0A        int leftSum = 0;%0A        int rightSum = 0;%0A        for (int i = 0; i %3C 3; i++) %7B%0A            leftSum += grid%5Brow + i%5D%5Bcol + i%5D;%0A            rightSum += grid%5Brow + i%5D%5Bcol + 2 - i%5D;%0A        %7D%0A        if (leftSum != rightSum) %7B%0A            return false;%0A        %7D%0A        return true
 ;%0A  

Solve problem 840