leetcode第221题(最大正方形)的本地IDE实现及变形

问题描述:

在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积。PS:本文也对只包含0的最大正方形面积进行了运算

示例:

输入: 

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0

输出: 4 
收获:    1.vector<string>v可以写成类似二维数组的形式 即v[i][j]代表一个字符    2.多维向量的声明及初始化line27:vector<vector<int>>dp(row,vector<int>(col,0))      或者vector<vector<int>>dp(row);for(int i=0;i<row;i++) dp[i].resize(col);    3.动态规划的知识程序:
//
//  main.cpp
//  lc221-最大正方形
//
//  Created by Apple on 2019/3/18.
//  Copyright © 2019年 wangyu. All rights reserved.
//

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
    vector<string>v;
    string s;
    int row;
    cin>>row;

    //int i=0;
    for(int i=0;i<row;i++){
        cin>>s;
        v.push_back(s);
    }
    int col=s.size();
    //int dp[row][col];
     /*求‘1’正方形的最大面积*/
   vector<vector<int>> dp(row, vector<int>(col, 0));//多维向量的声明和初始化
   // vector<vector<int>>dp(row);//另外一种多维向量声明和初始化方法
   // for(int i=0;i<row;i++)
     //   dp[i].resize(col);

        int res = 0;
    for (int i = 0; i < row; ++i) {
        for (int j = 0; j < col; ++j) {
            if (i == 0 || j == 0) { //初始化第一行、第一列
                dp[i][j] = v[i][j] - ‘0‘;
            }
            else if (v[i][j] == ‘1‘) {//动态规划
                dp[i][j] = min(dp[i - 1][j - 1], min(dp[i][j - 1], dp[i - 1][j])) + 1;
            }
            res =max(res, dp[i][j]);
        }
    }
    cout<< res*res<<endl;
    /*求‘0’正方形的最大面积*/
    vector<vector<int>> dd(row, vector<int>(col, 0));
    int ans=0l;
    for (int i = 0; i < row; ++i) {
        for (int j = 0; j < col; ++j) {
            if (i == 0 || j == 0) { //初始化第一行、第一列
                while(v[i][j]==‘0‘){
                    dd[i][j] = v[i][j] - ‘0‘+1;
                    break;

                }
                while (v[i][j]==‘1‘){
                    dd[i][j]=v[i][j]-‘1‘;
                    break;
                }

            }
            else if (v[i][j] == ‘0‘) {
                dd[i][j] = min(dd[i - 1][j - 1], min(dd[i][j - 1], dd[i - 1][j])) + 1;
            }
            ans=max(ans, dd[i][j]);
        }
    }
    cout<<ans*ans<<endl;
    cout<<max(ans*ans,res*res)<<endl;
}

 
 

原文地址:https://www.cnblogs.com/feiyuzaifei/p/10555575.html

时间: 2024-10-04 13:08:38

leetcode第221题(最大正方形)的本地IDE实现及变形的相关文章

LeetCode 第 73 题 (Set Matrix Zeroes)

LeetCode 第 73 题 (Set Matrix Zeroes) Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow up: Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea. A simple impro

leetcode中第一题twosum问题解答算法的可行性证明

leetcode中第一题twosum问题解答算法的可行性证明 一.引入 关于leetcode中第一题twosum问题,网上已有不少高人做出过解答,并提出了切实可行的算法实现.我在解答该题时参考了博客http://www.zixue7.com/article-9576-1.html的解答.为让读者更直观地阅读和理解本文,先简要摘录以上博客的内容如下: 题目还原 Two Sum Given an array of integers, find two numbers such that they a

Leetcode第五题_Longest Palindromic Substring

Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. Leetcode第5题,题目大概意思是给一个字符串,从中找出最长的回文串,所谓回文串,就是

LeetCode 第三题,Longest Substring Without Repeating Characters

题目: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest s

leetcode第9题-Palindrom Number

这是leetcode的第九题,相对来说比较简单,目的很简单,就是判断一个int型的数是不是回文数.但是有几点需要考虑: 负数应该没有回文数,要加判断!要注意额外的空间申请问题.判断是否是回文数势必要对一个数进行反转,反转的时候就要考虑溢出的问题.实现的代码如下: #include<stdio.h> bool isPalindrom(int x) { if(x<0) return false; else { int tmp=x; int sum=0; while(tmp) { sum=su

LeetCode 第 19 题 (Remove Nth Node From End of List)

LeetCode 第 19 题 (Remove Nth Node From End of List) Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the li

LeetCode 第 342 题(Power of Four)

LeetCode 第 342 题(Power of Four) Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it without loops/recursion? 题目很简单,

LeetCode第四题,Add Two Numbers

题目原文: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6

LeetCode 第 342 题(Power of Four)

LeetCode 第 342 题(Power of Four) Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it without loops/recursion? 题目非常eas