问题描述:

在一个由 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=;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, ));//多维向量的声明和初始化
// vector<vector<int>>dp(row);//另外一种多维向量声明和初始化方法
// for(int i=0;i<row;i++)
// dp[i].resize(col); int res = ;
for (int i = ; i < row; ++i) {
for (int j = ; j < col; ++j) {
if (i == || j == ) { //初始化第一行、第一列
dp[i][j] = v[i][j] - '';
}
else if (v[i][j] == '') {//动态规划
dp[i][j] = min(dp[i - ][j - ], min(dp[i][j - ], dp[i - ][j])) + ;
}
res =max(res, dp[i][j]);
}
}
cout<< res*res<<endl;
/*求‘0’正方形的最大面积*/
vector<vector<int>> dd(row, vector<int>(col, ));
int ans=0l;
for (int i = ; i < row; ++i) {
for (int j = ; j < col; ++j) {
if (i == || j == ) { //初始化第一行、第一列
while(v[i][j]==''){
dd[i][j] = v[i][j] - ''+;
break; }
while (v[i][j]==''){
dd[i][j]=v[i][j]-'';
break;
} }
else if (v[i][j] == '') {
dd[i][j] = min(dd[i - ][j - ], min(dd[i][j - ], dd[i - ][j])) + ;
}
ans=max(ans, dd[i][j]);
}
}
cout<<ans*ans<<endl;
cout<<max(ans*ans,res*res)<<endl;
}

 
 

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

  1. 【python】Leetcode每日一题-螺旋矩阵2

    [python]Leetcode每日一题-螺旋矩阵2 [题目描述] 给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix . ...

  2. leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  3. leetcode第37题--Count and Say

    题目:(据说是facebook的面试题哦) The count-and-say sequence is the sequence of integers beginning as follows:1, ...

  4. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  5. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  6. LeetCode的刷题利器(伪装到老板都无法diss你没有工作)

    在工程效率大行其道的今天,如果不会写点代码以后也不容易在测试圈混下去.今天给大家推荐一个LeetCode的刷题利器,可以伪装到连你老板在这里走过去都无法确认你是在干活呢,还是在干活呢. LeetCod ...

  7. LeetCode每天一题之两数之和

    这个LeetCode刷题系列的博客权当是为自己记一下笔记吧.博客系列会从LeetCode的第一题开始刷,同时会从零开始学习[因为我就是零/(ㄒoㄒ)/~~].同时,如果有写错的地方,希望大佬们在评论区 ...

  8. leetcode第三题

    leetcode第三题: 题目: 给定一个字符串,找出不含有重复字符的最长子串的长度. 源码(使用java语言): class Solution { public int lengthOfLonges ...

  9. [LeetCode] 系统刷题5_Dynamic Programming

    Dynamic Programming 实际上是[LeetCode] 系统刷题4_Binary Tree & Divide and Conquer的基础上,加上记忆化的过程.就是说,如果这个题 ...

随机推荐

  1. C语言实现通用链表初步(二)

    接着上次的内容,我们继续! 还是无头单向非循环链表.假如要删除某个节点,如何实现? //删除成功返回0,失败返回-1 int slist_del(struct node_info *node, str ...

  2. PHP中的header()函数

    PHP header 函数的用法及其注意事项 void header ( string $string [, bool $replace = true [, int $http_response_co ...

  3. 导出CSV

    public FileResult ExportExcel() { var sbHtml = new StringBuilder(); sbHtml.Append("<table bo ...

  4. WinPcap抓取数据包

    #WinPcap和Libpcap的最强大的特性之一,是拥有过滤数据包的引擎. 它提供了有效的方法去获取网络中的某些数据包,这也是WinPcap捕获机制中的一个组成部分. 用来过滤数据包的函数是#

  5. h5:erver-Sent Events

    对于一般的 Web 应用开发,大多数开发人员并不陌生.在 Web 应用中,浏览器和服务器之间使用的是请求 / 响应的交互模式.浏览器发出请求,服务器根据收到的请求来生成相应的响应.浏览器再对收到的响应 ...

  6. The eleventh day

    What's the damage? Thanks so much for fixing the break on my car . What's the damage for the work yo ...

  7. Javascript的map与forEach的区别

    原理: 高级浏览器支持forEach方法语法:forEach和map都支持2个参数:一个是回调函数(item,index,list)和上下文: forEach:用来遍历数组中的每一项:这个方法执行是没 ...

  8. Git由来

    很多人都知道,Linus在1991年创建了开源的Linux,从此,Linux系统不断发展,已经成为最大的服务器系统软件了. Linus虽然创建了Linux,但Linux的壮大是靠全世界热心的志愿者参与 ...

  9. 859. Buddy Strings (wrong 4 times so many cases to test and consider) if else**

    Given two strings A and B of lowercase letters, return true if and only if we can swap two letters i ...

  10. react及flux架构范例Todomvc分析

    react及flux架构范例Todomvc分析 通过分析flux-todomvc源码,学习如何通过react构建web程序,了解编写react应用程序的一般步骤,同时掌握Flux的单向数据流动架构思想 ...