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=;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实现及变形的更多相关文章
- 【python】Leetcode每日一题-螺旋矩阵2
[python]Leetcode每日一题-螺旋矩阵2 [题目描述] 给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix . ...
- leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
- leetcode第37题--Count and Say
题目:(据说是facebook的面试题哦) The count-and-say sequence is the sequence of integers beginning as follows:1, ...
- 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 + ...
- 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 ...
- LeetCode的刷题利器(伪装到老板都无法diss你没有工作)
在工程效率大行其道的今天,如果不会写点代码以后也不容易在测试圈混下去.今天给大家推荐一个LeetCode的刷题利器,可以伪装到连你老板在这里走过去都无法确认你是在干活呢,还是在干活呢. LeetCod ...
- LeetCode每天一题之两数之和
这个LeetCode刷题系列的博客权当是为自己记一下笔记吧.博客系列会从LeetCode的第一题开始刷,同时会从零开始学习[因为我就是零/(ㄒoㄒ)/~~].同时,如果有写错的地方,希望大佬们在评论区 ...
- leetcode第三题
leetcode第三题: 题目: 给定一个字符串,找出不含有重复字符的最长子串的长度. 源码(使用java语言): class Solution { public int lengthOfLonges ...
- [LeetCode] 系统刷题5_Dynamic Programming
Dynamic Programming 实际上是[LeetCode] 系统刷题4_Binary Tree & Divide and Conquer的基础上,加上记忆化的过程.就是说,如果这个题 ...
随机推荐
- CentOS 6.5 & 7 的网络YUM源配置
中国科技大学CentOS 6.5的网络源 [base]name=CentOS-$releasever - Base#mirrorlist=http://mirrorlist.centos.org/?r ...
- Unity3D跨平台动态库编译---记kcp基于CMake的各平台构建实践
一 为什么需要动态库 1)提供原生代码(native code)的支持,也叫原生插件,但是我实践的是c/cpp跨平台动态库,这里不具体涉及安卓平台java库和ios平台的objectc库构建. 2)某 ...
- Jersey框架简介
一 Jersey简介 开发RESTful WebService意味着支持在多种媒体类型以及抽象底层的客户端-服务器通信细节,如果没有一个好的工具包可用,这将是一个困难的任务 为了简化使用JAVA开发R ...
- UITableView分隔线
问题1: 在ios中使用UITableView时,当行数较少是,可能一屏幕能显示完全所有行,这时候会出现下面的问题,显示多余的分隔线 图如下: 解决方案: //解决方案1 //添加如下代码 -(CGF ...
- 在CentOS上配置tomcat服务
# hapday start 2016-02-04 #!/bin/bash # description: Tomcat Start Stop Restart # processname: tomcat ...
- (开发)bable - es6转码
参考:http://www.ruanyifeng.com/blog/2016/01/babel.html Babel Babel是一个广泛使用的转码器,可以将ES6代码转为ES5代码,从而在现有环境执 ...
- 表单验证插件及一些属性的用法 validate
注:必须配合jq使用. 基本语法 例如有如下: <form action="" method="post" id="demoForm" ...
- c#和c++的运算符优先级
闲来无聊乱写代码.发现基础的东西有的时候也非常的抽象.不信?那来看看下面这条语句: ; ; j = i = -i++; 如果上述代码是vc++代码,那么输出结果是: i=- j=- 请按任意键继续. ...
- Unity3D 调用Android与IOS的剪贴板
Unity3D剪贴板 最近遇到一个需要调用Android与IOS设备本身剪贴板的需求,就是在Unity中,要将文本复制到设备本身的剪贴板中,然后在其他应用程序中都能粘贴. 最开始在网上查到的方式是使用 ...
- R.java文件无法自动生成的问题
如果出现R.java文件无法自动生成的问题,同时Console窗口提示下列信息: Android requires compiler compliance level 5.0 or 6.0. Foun ...