当前位置:网站首页>【LeetCode】221.最大正方形
【LeetCode】221.最大正方形
2022-06-10 01:08:00 【LawsonAbs】
0.总结
- 注意
copy的使用。
1.题目
2.思想
- (1)dp[i][j] 表示以 matrix[i][j] 为结尾得到的最大正方形边长
- (2)得到递推式:
min(dp[i-1][j-1],dp[i-1][j],dp[i][j-1])+1
3.代码
import copy
class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
dp = copy.deepcopy(matrix)
m,n = len(matrix),len(matrix[0])
# 修改预处理的部分
for i in range(m):
for j in range(n):
dp[i][j] = int(dp[i][j])
dp.append([0]*len(matrix[0])) # 添加新的一行
for i in range(m+1):
dp[i].append(0)
# print(dp)
# print(matrix)
# 找出最大边长
span = 0
for i in range(m):
for j in range(n):
if matrix[i][j] == "1":
dp[i][j] = min(dp[i-1][j-1],dp[i-1][j],dp[i][j-1])+1
span = max(span,dp[i][j])
return span**2
# return dp[m-1][n-1]
上面这个代码是使用了额外数组空间的。可以再进一步整合只使用 matrix 也是可以的。
''' 思想 (1)dp[i][j] 表示以 matrix[i][j] 为结尾得到的最大正方形边长 (2)得到递推式: dp[i][j] = dp[] '''
import copy
class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
m,n = len(matrix),len(matrix[0])
# 修改预处理的部分
for i in range(m):
for j in range(n):
matrix[i][j] = int(matrix[i][j])
matrix.append([0]*len(matrix[0])) # 添加新的一行
for i in range(m+1):
matrix[i].append(0)
# 找出最大边长
span = 0
for i in range(m):
for j in range(n):
if matrix[i][j] == 1:
matrix[i][j] = min(matrix[i-1][j-1],matrix[i-1][j],matrix[i][j-1])+1
span = max(span,matrix[i][j])
return span**2
# return dp[m-1][n-1]
边栏推荐
- Shell Eval usage explanation command replacement
- MySQL transaction
- MySQL - transaction attributes
- H3C IRF configuration example
- TCP 连接中的keep-alive
- ESRI integrates Lightbox data to extend geocoding in Canada
- What is the difference between encryptors and database encryption products?
- Regular expression does not contain a string
- 把 GPL 视作“病毒”?请停止污名化 GPL
- JVM——》类文件
猜你喜欢

从转载阿里开源项目 Egg.js 技术文档引发的“版权纠纷”,看宽松的 MIT 许可该如何用?

【ICLR 2022】Towards Continual Knowledge Learning of Language Models

Flink CDC + Hudi 海量数据入湖在顺丰的实践

MySQL - isolation level of transactions

How can win11 directly return to the desktop?

Node red series (II and III): develop a Gaode map panel dashboard in node red

Where should middle-aged test development engineers go? The unknown is tomorrow, break the label

Mysql——》如何解决数据的读一致性问题

Teaching Broad Reasoning Skills via Decomposition-Guided Contexts

国内现货白银有哪些好技术:常见指标的简单用法
随机推荐
MySQL - view index size
CocosCreator旧活新整-合成大粽子
MySQL transaction
What are the reasons for frequent channel drops in easycvr cascaded video convergence platform?
从转载阿里开源项目 Egg.js 技术文档引发的“版权纠纷”,看宽松的 MIT 许可该如何用?
Reprint the Alibaba open source project egg JS technical documents cause "copyright disputes". How to use the loose MIT license?
中年测试开发工程师该何去何从?未知的是明天,打破标签......
JVM -- class compilation process
ThingsBoard教程(十八):tb规则引擎的概述
騰訊Libco協程開源庫 源碼分析(一)---- 下載Libco 編譯安裝 嘗試運行示例代碼
Practice of Flink CDC + Hudi massive data entering the lake in SF
Xargs command details, the difference between xargs and pipeline
孙宇晨等收购Poloniex,公链交易所双轮驱动波场生态
写入速度提升数十倍,TDengine 在拓斯达智能工厂解决方案上的应用
Cocoscreator old, live and new - synthetic large zongzi
图片批量下载 +图片马赛克:多张图片组成端午安康
Locust: a powerful tool for microservice performance testing
Mysql——》如何解决数据的读一致性问题
Esri整合LightBox数据以扩展加拿大境内的地理编码
地图瓦片数据的多种利用形式以及瓦片数据的浏览显示