class Solution { public int countCornerRectangles(int[][] grid) { if(grid.length < 2 || grid[0].length < 2) return 0; int m = grid.length, n = grid[0].length, res = 0; int[][] dp = new int[n][n]; // dp[i][j] 表示这一行[i,j]两个点都是1且向上数存在也是两个1的对数 for(int i = 0; i < m; i++) { for(int j = 0; j < n; j++) { if(grid[i][j] == 0) continue; for(int k = j + 1; k < n; k++) { if(grid[i][k] == 1) { res += dp[j][k]; dp[j][k]++; } } } } return res; } }