Fork me on GitHub

1139. 最大的以 1 为边界的正方形

class Solution {
	public int largest1BorderedSquare(int[][] grid) {
		int ans = 0;
		int n = grid.length;
		int m = grid[0].length;
		int[][] ud = new int[n][m];
		int[][] lr = new int[n][m];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (i == 0) // 进行数据处理,左右连续或者上下连续的一串将在grid保存
					ud[i][j] = (grid[i][j] == 1 ? 1 : 0);
				else
					ud[i][j] = (grid[i][j] == 1 ? 1 + ud[i - 1][j] : 0);

				if (j == 0)
					lr[i][j] = (grid[i][j] == 1 ? 1 : 0);
				else
					lr[i][j] = (grid[i][j] == 1 ? 1 + lr[i][j - 1] : 0);
				for (int k = 1; k <= Math.min(i, j) + 1; k++) {
					if (lr[i][j] < k)
						continue;
					if (ud[i][j] < k)
						continue;

					if (lr[i - k + 1][j] < k) // >= k 都能将k进行判断
						continue;              //continue 也是恰到好处
					if (ud[i][j - k + 1] < k)
						continue;
					ans = Math.max(ans, k);
				}
			}
		}
		return ans * ans;
	}
}
posted @ 2019-08-04 14:59  cznczai  阅读(211)  评论(0编辑  收藏  举报