221-最大正方形

Java实现 LeetCode 221 最大正方形 221. 最大正方形 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积。 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4 PS: 当我们判断以某个点为正方形右下角时最大的正方形时,那它的上方,左方和左上方三个点也一定是某个正方形的右下角,否则该点为右下角的正方形最大就是它自己了。这是定性的判断,那具体的最... 阅读详情

题目

在一个由 ‘0’ 和 ‘1’ 组成的二维矩阵内,找到只包含 ‘1’ 的最大正方形,并返回其面积。

示例 1:

输入:matrix = [[“1”,“0”,“1”,“0”,“0”],[“1”,“0”,“1”,“1”,“1”],[“1”,“1”,“1”,“1”,“1”],[“1”,“0”,“0”,“1”,“0”]]
输出:4
示例 2:

输入:matrix = [[“0”,“1”],[“1”,“0”]]
输出:1
示例 3:

输入:matrix = [[“0”]]
输出:0

题解

const maximalSquare = function(matrix) {
    if (!matrix.length) return 0
    const dp = new Array(matrix.length + 1).fill(0).map(() => new Array(matrix[0].length + 1).fill(0))
    let maxLen = 0
    for (let i = 1; i < dp.length; i++) { //以dp的长度遍历!以防边界情况!
        for (let j = 1; j < dp[0].length; j++) {
            if (matrix[i - 1][j - 1] === '1') {
                dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1
                maxLen = Math.max(dp[i][j], maxLen)
            }
        }
    }
    return maxLen * maxLen
}

笔记:

  1. 定义 dp[i][j]:以坐标 (i,j) 为右下角的最大正方形边长。

    (i,j) 为 0 时,无法构成正方形,dp[i][j] = 0

    (i,j) 为 1 时,dp[i][j] = min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1

    一个正方形的最大边长决定于它左方、上方、斜上方的位置所能形成的最大正方形的边长,即:三者的最小值 + 自身的长度 1。

    为了避免边界条件判断,可以将 dp 数组的长和宽都增加 1。

Leetcode—221.Maximal Square 最大正方形 Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 阅读详情

相关推荐

`全网最细!! 入门必看Git教程链接:https://gitee.com/all-about-git`

全网 Git 入门教程 gitee.com。如果显示The requested URL returned error: 403错误,那就表示没有和window本地建立token令牌

qq_37855368的博客 3749

LeetCode 221最大正方形

LeetCode 221最大正方形 题目简述:在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积。 示例:输入: [10100101111111110010] \left[\begin{matrix}1 & 0 & 1 & 0 & 0 \\1 & 0 & 1 & 1 & 1 \\1 & 1 & 1 & 1 & 1\\1 & 0 & 0 & 1 &am

XBlog 291

ADS交叉耦合振荡器VCO相位噪声优化仿真实践

本文详细介绍了在ADS软件中搭建和优化交叉耦合VCO相位噪声的完整仿真实践。通过谐波平衡与瞬态仿真分析电路性能,并重点阐述了通过提升谐振腔Q值、优化晶体管工作点及改善尾电流源来有效降低相位噪声的具体方法,为射频电路设计提供了实用指南。

weixin_29235923的博客 638

【中等】力扣算法题解析LeetCode221:最大正方形

题目要求在由0和1组成的二维矩阵中找到最大全1正方形的面积。使用动态规划优化空间,通过一维数组dp记录当前行各位置作为右下角时的最大边长。状态转移方程为dp[j] = min(dp[j-1], dp[j], pre) + 1(pre为左上角值),边界处理单独初始化首行首列。遍历时更新最大边长maxSide,最终返回maxSide²。Java实现代码已通过力扣测试,执行高效。

ylumwd的博客 840

UHPC的疲劳计算——兼论ModelCode2010的适用性

ModelCode2010的混凝土疲劳计算方法是适用于UHPC的。

George的博客 1325

深入解析SVPWM七段式与五段式调制的电压波形差异及优化策略

本文深入解析了SVPWM七段式与五段式调制在相电压和线电压波形上的核心差异。七段式波形对称、谐波含量低,适合追求静音与性能的场景;五段式开关次数少、损耗低,适用于对效率与散热要求高的应用。文章提供了基于谐波与损耗权衡的优化策略与选型指南。

weixin_29216957的博客 443

LeetCode 221.最大正方形 解法

/** LeetCode 221.最大正方形 形成2*2的小正方形,基础上 [ [a, b] [c, d(?)] ] 若b >= a && c >= a说明d所在的一个正方形可以在a的基础上,扩充一行一列,变为长宽为a+1的正方形 否则d为Min(b, c)的最小值为长宽的正方形,增加一行一列,变为长宽为Min(b, a)+1的正方形 */ func maximalSquare(matrix [][]byte) int { rowLen := len(matrix) .

NLSQQ的博客 299

AJ-Report开源数据大屏存在远程命令执行漏洞

该平台可以通过post方式在validationRules参数对应值中进行命令执行,可以获得服务器权限,登陆管理后台接管大屏。如果被攻击者利用,危害后果十分严重。

swordheart的博客 388

221. 最大正方形

题目: 221. 最大正方形 题解: 代码: public class code221 { public static int maximalSquare(char[][] matrix) { // base condition if(matrix == null || matrix.length == 0 || matrix[0].length == 0) { return 0; }

dev_zyx的博客 527

plc secs半导体通讯协议 PLC 与MES的SECS/GEM通讯方案

符合SEMI E4, E5, E30, E37 的 SECS/GEM通讯方案 专业稳定的产品,持续运行5年都不出错 遵循 SEMI 人机交互设计,提供触摸屏支持让你触手可及。 毫米级的响应,单个messge在5ms以内 MES与PLC之间达到实时性,如采集实时性的数据 每秒支持100条Message以上 快速搭建PLC与MES之间的SECS/GEM桥梁,只需简单配置数据 SECS/GEM介绍 遵循 SEMI 人机交互设计,提供触摸屏支持让你触手可及。 增删改上报数据配置简单,并且一目了然 大量的数据处理.

SECS/GEM 6366

电子工程师必看:2024年IPC-A-610J和J-STD-001J新标准到底更新了啥?

本文深入解析2024年IPC-A-610J和J-STD-001J新标准的核心更新,重点关注电子制造领域的验收标准与工艺参数变化。新版标准强化了高密度组装和微焊接工艺的规范,包括焊料润湿角调整、BGA空洞率严格化等关键变更,为电子工程师提供实用指南。同时关联IPC-7711/21D和IPC-2221C的配套更新,助力企业快速适应新标准要求。

weixin_33694620的博客 795

LeetCode221——最大正方形

我的LeetCode代码仓:https://github.com/617076674/LeetCode 原题链接:https://leetcode-cn.com/problems/maximal-square/description/ 题目描述: 知识点:动态规划 思路:动态规划 状态定义: f(x, y) -------- 以(x, y)坐标为右下角的只含1的最大正方形面积 状...

清风阁 542

STM32F407用FSMC+DMA驱动LVGL,3.5寸屏流畅度翻倍实战(附CubeMX配置避坑)

本文详细介绍了如何在STM32F407上通过FSMC+DMA驱动LVGL,显著提升3.5寸屏的显示流畅度。从FSMC与DMA的配置原理到LVGL驱动层适配,提供了完整的优化方案和实测数据,帮助开发者解决界面卡顿问题,实现流畅度翻倍的效果。

weixin_30872499的博客 559

LeetCode221 最大正方形

LeetCode221 最大正方形 网址 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积。 class Solution { public: int maximalSquare(vector<vector<char>>& matrix) { /** dp[i][j]表示以第i行第j列为右下角所能构成的最大正方形边长, 则递推式为: dp[i][j] = 1 + min(dp[i-1][j-1],

J k l的博客 801

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

LeetCode 221. 最大正方形 | Python

使用动态规划,找出最长边,进而解决《221. 最大正方形》问题

"大梦三千秋的博客 1289

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

LeetCode之221. 最大正方形

题目 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积。 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/maximal-square 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 分析 动态规划思想:将一个问题划分为N个子问题,逐个解析子问题,前一个子问题的输出作为下一个子问题

剑指Offer编程题 706

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

leetcode221---最大正方形

leetcode221—最大正方形原题链接 题意简述 输入: matrix [][]char,只包含’0’和’1’的矩阵; 操作: 找出只包含’1’的最大正方形,计算其面积 输出: max int,该面积 解法分析 动态规划 dp[i][j]: 以matrix[i][j]为正方形右下角且只包含'1'的最大正方形边长; 状态转移方程: dp[i][j]=min(dp[i-1][j-1],dp[...

Again的博客 343

Leetcode 221-最大正方形

在一个由 ‘0’ 和 ‘1’ 组成的二维矩阵内,找到只包含 ‘1’ 的最大正方形,并返回其面积。提示:matrix[i][j] 为 ‘0’ 或 ‘1’

Helene1996的博客 773

Leetcode第221题 最大正方形

输入:matrix = [[“1”,“0”,“1”,“0”,“0”],[“1”,“0”,“1”,“1”,“1”],[“1”,“1”,“1”,“1”,“1”],[“1”,“0”,“0”,“1”,“0”]]题目描述:在一个由 ‘0’ 和 ‘1’ 组成的二维矩阵内,找到只包含 ‘1’ 的最大正方形,并返回其面积。我们用num[ i ][ j ]来表示以点(i,j)为右下角的矩阵的边长。输入:matrix = [[“0”,“1”],[“1”,“0”]]尝试通过于(i,j)相邻的位置求出数组中点(i,j)的长度。

weixin_66164496的博客 189

LeetCode 221 —— 221. 最大正方形

但是增大的面积又必须依赖于这三个值的最小值,因为一个方向大是没用的,必须是三个方向都能延续才行,这个整体的正方形才能继续增大1。比如你看 (3, 3) 这个点,其左方、左上方都是3,但是上方是0,所以这个点的正方形就组不成了,只能“从头再来“了。在一个由 ‘0’ 和 ‘1’ 组成的二维矩阵内,找到只包含 ‘1’ 的最大正方形,并返回其面积。其实可以想到的是,如果某个点想接着组成正方形,那么它的左上方、上方、左方都得至少是1。都是1的情况下,说明这个点可以延续上方、左方、左上方的正方形,继续增大一个。

刘伟佳的博客 770

Leetcode 221. 最大正方形

Leetcode 221. 最大正方形 题目 在一个由 ‘0’ 和 ‘1’ 组成的二维矩阵内,找到只包含 ‘1’ 的最大正方形,并返回其面积。 m == matrix.length n == matrix[i].length 1

专注如一的博客 849

LeetCode 221. 最大正方形

LeetCode 221. 最大正方形 一、题目详情 在一个由 '0' 和 '1' 组成的二维矩阵内,找到只包含 '1' 的最大正方形,并返回其面积。 示例 1: 输入:matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]] 输出:4 示例 2: 输入:matrix = [["0","1"],["1","0"]] 输出:1 示例 3: 输入:matrix =

jrrhjww的博客 115

LeetCode221. 最大正方形

LeetCode221. 最大正方形题目描述示例1思路 题目描述 听说这道题是字节的面试题,于是一时兴起就开始做,边做边发呆。。。效率贼慢,不过最后还是写出来了(尼玛这是中等题?那我之前做的那些困难题,不也是中等题了?) 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积。 示例1 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0...

weixin_41716072的博客 383

LeetCode第221题_最大正方形

LeetCode 第221题要求在一个由 '0' 和 '1' 组成的二维矩阵中,找到只包含 '1' 的最大正方形,并返回其面积。该问题可以通过动态规划高效解决。定义 dp[i][j] 表示以 matrix[i][j] 为右下角的正方形的最大边长。状态转移方程为:如果 matrix[i][j] = '1',则 dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1;否则 dp[i][j] = 0。通过遍历矩阵,更新 dp 数组,最终找到最大正方形的边长

qq_40263592的博客 1574

Leetcode 221.最大正方形(Maximal Square)

Leetcode 221.最大正方形 1 题目描述(Leetcode题目链接)   在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积。 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4 2 题解   本题为动态规划问题,起初的想法是构建一个DPDPDP矩阵,矩阵的元素含义为以该元素为右下角的最大正方形的面积...

qq_39378221的博客 241
上一篇: 215-数组中的第K个最大元素
下一篇: 226-翻转二叉树
codrab
博客等级 码龄10年 15粉丝 98原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

codrab

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值