不确定是否算暴力解题
最大正方形
http://www.nowcoder.com/questionTerminal/0058c4092cec44c2975e38223f10470e
从最理想的正方形开始依次寻找;
for (int length = Math.min(arr.length, arr[0].length); length > 1; length--) { for (int y = 0, y_length = arr.length; y < y_length - length + 1; y++) { for (int x = 0, x_length = arr[y].length; x < x_length - length + 1; x++) { if (validate(length, arr, x, y)) { System.out.println("size = " + length * length); return; } } } } System.out.println("size = 1");
接着是一个用来验证当前长度是否符合正方形要求的逻辑
private static boolean validate(int length, int[][] arr, int x, int y){ for (int i = y, i_l = y+length; i < i_l; i++) { for (int j = x, j_l = x+length; j < j_l; j++) { if (arr[i][j] != 1) return false; } } return true; }