java.lang.Object
g1201_1300.s1277_count_square_submatrices_with_all_ones.Solution

public class Solution extends Object
1277 - Count Square Submatrices with All Ones.<p>Medium</p> <p>Given a <code>m * n</code> matrix of ones and zeros, return how many <strong>square</strong> submatrices have all ones.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> matrix = [ [0,1,1,1], [1,1,1,1], [0,1,1,1] ]</p> <p><strong>Output:</strong> 15</p> <p><strong>Explanation:</strong></p> <p>There are <strong>10</strong> squares of side 1.</p> <p>There are <strong>4</strong> squares of side 2.</p> <p>There is <strong>1</strong> square of side 3.</p> <p>Total number of squares = 10 + 4 + 1 = <strong>15</strong>.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> matrix = [ [1,0,1], [1,1,0], [1,1,0] ]</p> <p><strong>Output:</strong> 7</p> <p><strong>Explanation:</strong></p> <p>There are <strong>6</strong> squares of side 1.</p> <p>There is <strong>1</strong> square of side 2.</p> <p>Total number of squares = 6 + 1 = <strong>7</strong>.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= arr.length <= 300</code></li> <li><code>1 <= arr[0].length <= 300</code></li> <li><code>0 <= arr[i][j] <= 1</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • countSquares

      public int countSquares(int[][] matrix)