您的位置:首页 > 其它

[LeetCode]--221. Maximal Square

2016-08-06 18:46 381 查看


public class Solution {
public int maximalSquare(char[][] a) {
if (a == null || a.length == 0 || a[0].length ==0)  return 0;
int max = 0, m = a.length, n = a[0].length;
// dp[i,j] represents the length of the square whose lower-right is
//located at (i, j) , dp[i,j] = min{dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]}, if a[i][j] = 1,
// add 1
int[][] dp = new int[m + 1][n + 1];
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (a[i - 1][j - 1] == '1') {
dp[i][j] = Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1])) + 1;
max = Math.max(max, dp[i][j]);        //Think carefully about it!
}
}
}
return (int)(Math.pow(max, 2));
}
}


It’s Dynamic Programming!

Reference: https://discuss.leetcode.com/topic/18482/accepted-clean-java-dp-solution
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: