题解 | #最大正方形# 一维DP

最大正方形

http://www.nowcoder.com/practice/0058c4092cec44c2975e38223f10470e

class Solution {
public:
    /**
     * 最大正方形
     * @param matrix char字符型vector<vector<>> 
     * @return int整型
     */
    int solve(vector<vector<char> >& matrix) {
        // write code here
        if(matrix.empty()) return 0;
        int row = matrix.size(),col = matrix[0].size();
        //vector<vector<int>> dp(row,vector<int>(col));
        vector<int> dp(col+1);
        int res = 0;
        for(int i=1;i<=row;i++){
            int pre = 0;//记录左上
            for(int j=1;j<=col;j++){
                int tem = dp[j];//记录当前
                if(matrix[i-1][j-1] == '1'){
                    dp[j] = min(dp[j-1],min(dp[j],pre))+1;
                    res = max(res,dp[j]);
                }else dp[j] = 0;
                pre = tem;//当前成为下一次的左上
            }
        }
        return res*res;
    }
};
全部评论

相关推荐

计算机遨游小白:可能是后端没HC了,用前端的HC招
投递京东等公司9个岗位
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务