leetcode-Maximal Square
来源:互联网 发布:平面图纸设计软件 编辑:程序博客网 时间:2024/05/16 18:23
这里本来要申请一个动态的二维数组的 可是没有想出怎么申请一个动态的二维数组 就申请了一个一伟数组当二维数组用了
dp[i][j] 代表以i j为右下角的最大面积的正方形的边长
if(matrix[i][j]==’1’)dp[i][j]=min(dp[i-1][j-1],dp[i-1][j],dp[i][j-1])+1
else dp[i][j]=0;
下面是提交的代码
#include <vector>#include <iostream>#include <algorithm>using namespace std;/*dp[i*ml+j] 代表以i,j为右下角的正方形的最大边长*/class Solution {public: int maximalSquare(vector<vector<char>>& matrix) { int temp; xlength = matrix.size(); if (xlength != 0) { ylength = matrix[0].size(); dp = new int[xlength*ylength]; for (int i = 0; i < xlength; i++) { if (matrix[i][0] == '1') valueIs(i, 0, 1); else valueIs(i, 0, 0); } for (int i = 0; i < ylength; i++) { if (matrix[0][i] == '1') valueIs(0, i, 1); else valueIs(0, i, 0); } for (int i = 1; i < xlength; i++) for (int j = 1; j < ylength; j++) { if (matrix[i][j] == '1') temp = minOfThree(valueAt(i - 1, j - 1), valueAt(i - 1, j), valueAt(i, j - 1)) + 1; else temp = 0; valueIs(i, j, temp); } } int max = 0; for (int i = 0; i < xlength; i++) for (int j = 0; j < ylength; j++) { if (max < valueAt(i, j)) max = valueAt(i, j); } temp = max*max; return temp; } ~Solution() { delete[]dp; }private: int minOfTwo(int x, int y) { return min(x, y); } int minOfThree(int x, int y, int w) { int temp = min(x, y); return min(temp, w); } void valueIs(int i, int j,int value) { dp[i*ylength + j] = value; } int valueAt(int i, int j) { if (i <= xlength-1 && j <= ylength-1) return dp[i*ylength + j]; else return -1; } int *dp; int xlength; int ylength;};
下面是自己测试的代码
int main()
{
int xlength, ylength;
int temp;
vector
0 0
- 【LeetCode】Maximal Rectangle && Maximal Square
- Leetcode:Maximal Square & Maximal Rectangle
- [leetcode] Maximal Square
- Maximal Square Leetcode Java
- [leetcode] Maximal Square
- leetcode 221: Maximal Square
- LeetCode Maximal Square
- Maximal Square - LeetCode 221
- #leetcode#Maximal Square
- LeetCode Maximal Square
- [LeetCode] Maximal Square
- [LeetCode]Maximal Square
- leetcode Maximal Square
- leetcode Maximal Square
- Leetcode 221 Maximal Square
- leetCode(27):Maximal Square
- [leetCode] Maximal Square
- Leetcode 221 Maximal Square
- asp.net项目无法加载web目录下的文件
- LeetCode 257 Binary Tree Paths
- 【Unity3d】【项目学习心得】从资源服务器下载资源(三)
- MUI之父页面刷新
- MPAndroidChart 教程:概述
- leetcode-Maximal Square
- 前端开发面试总结HTML、CSS部分
- iOS开发——使用MBProgressHUD来增加用户体验
- 手机用户对手机游戏的偏好调查
- Two Sum
- 前端学习
- 常用算法
- 超详细mysql left join,right join,inner join用法分析
- Markdown编辑器写博客