问题描述:

在一个由 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. [转] js 实现table每列可左右拖动改变列宽度

    <!DOCTYPE HTML>   <html>   <head>   <meta charset="gbk">   <tit ...

  2. Windows64bit-plsqldeveloper-install the easiest way

    The easiest way to add a 32 Bit Oracle Client: 1.Download the Oracle 11g or 12c Instant Client(http: ...

  3. MVC4 过滤器使用和怎样控制全部action和部分action

    MVC中的过滤器分四种分别为:IActionFilter(动作过滤器), IAuthorizationFilter(授权过滤器), IExceptionFilter(异常过滤器), IResultFi ...

  4. SpringBoot系列之——整合JPA、mysql

    一.JPA      1. 概念:JPA顾名思义就是Java Persistence API的意思,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. 2. ...

  5. 通用代码——makefile文件

    ver=debug ifeq ($(ver),debug) TARGET = testmain_d FLAG=-g -D debug else TARGET = testmain_r FLAG=-O3 ...

  6. Hibernate课程 初探一对多映射5-1 课程总结

    1 单方一对多 xml one-to-many 配置 实体类   一方添加保存多方集合 2 单方多对一 xml many-to-one 配置 实体类   多方添加保存一方引用 3 常用属性 inver ...

  7. Unified Service Desk Overview

    As we implement CRM in enterprise, we usually integrate with many other information system such as E ...

  8. One special dictionary

    由于项目一个功能需要,可以将关键字的值叠加加来,最终可以获取对这些关键字都做了些什么操作. Generic Programming is very powerful. /// <summary& ...

  9. 腾讯bugly 映射用法

    package com.tencent.bugly.agent; import android.app.Activity; import android.content.Context; import ...

  10. Linq to Sql 左连接 , 取右表可能为 null的 int类型字段

    linq to sql , linq to entity 遇到一个问题, 主表, 从表 一对一 关系,  主表有记录, 从表 可能没有记录. 现在要查询 主表+从表 的某几个字段. 从表字段 有的是 ...