当前位置:网站首页>模拟卷Leetcode【普通】1314. 矩阵区域和
模拟卷Leetcode【普通】1314. 矩阵区域和
2022-07-06 06:15:00 【邂逅模拟卷】
1314. 矩阵区域和
给你一个 m x n 的矩阵 mat 和一个整数 k ,请你返回一个矩阵 answer ,其中每个 answer[i][j] 是所有满足下述条件的元素 mat[r][c] 的和:
i - k <= r <= i + k,
j - k <= c <= j + k 且
(r, c) 在矩阵内。
示例 1:
输入:mat = [[1,2,3],[4,5,6],[7,8,9]], k = 1
输出:[[12,21,16],[27,45,33],[24,39,28]]
示例 2:
输入:mat = [[1,2,3],[4,5,6],[7,8,9]], k = 2
输出:[[45,45,45],[45,45,45],[45,45,45]]
提示:
m == mat.length
n == mat[i].length
1 <= m, n, k <= 100
1 <= mat[i][j] <= 100
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/matrix-block-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码:
import numpy as np
from leetcode_python.utils import *
from scipy import signal
class Solution:
def __init__(self):
pass
def matrixBlockSum(self, mat: List[List[int]], k: int) -> List[List[int]]:
data = np.array(mat)
h,w = data.shape
res = [[int(np.sum(data[max(0,i-k):min(h,i+k+1),max(0,j-k):min(w,j+k+1)])) for j in range(w)] for i in range(h)]
return res
def matrixBlockSum_conv(self, mat: List[List[int]], k: int) -> List[List[int]]:
"""但是不知道为什么结果不对,尤其卷积核超过mat的时候"""
data = np.array(mat)
print(data)
nurcle = np.ones((1+k*2,1+k*2)).astype(np.uint32)
# nurcle[k][k]=0
print(nurcle)
res = signal.convolve2d(data, nurcle, boundary='fill', fillvalue=0).astype(np.uint8)
# res = signal.convolve2d(data, nurcle, 'same').astype(np.uint8)
print(res)
return res.tolist()
def test(data_test):
s = Solution()
return s.matrixBlockSum(*data_test)
def test_obj(data_test):
result = [None]
obj = Solution(*data_test[1][0])
for fun, data in zip(data_test[0][1::], data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
# [[[1,2,3],[4,5,6],[7,8,9]],2],
[[[67,64,78],[99,98,38],[82,46,46],[6,52,55],[55,99,45]],3],
# [[[1,1,1],[1,2,1],[1,1,1],[1,1,1],[1,1,1]],3],
# [[[1,1,1,1,1],[1,2,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1]],5],
]
for data_test in datas:
t0 = time.time()
print('-' * 50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{
time.time() - t0}s')
备注:
GitHub:https://github.com/monijuan/leetcode_python
CSDN汇总:模拟卷Leetcode 题解汇总_卷子的博客-CSDN博客
可以加QQ群交流:1092754609
leetcode_python.utils详见汇总页说明
先刷的题,之后用脚本生成的blog,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- Expose the serial fraudster Liu Qing in the currency circle, and default hundreds of millions of Cheng Laolai
- Request forwarding and redirection
- 對數據安全的思考(轉載)
- Luogu p1460 [usaco2.1] healthy Holstein cows
- G - Supermarket
- Company video accelerated playback
- Caused by:org. gradle. api. internal. plugins . PluginApplicationException: Failed to apply plugin
- 黑猫带你学UFS协议第4篇:UFS协议栈详解
- GTSAM中李群的运用
- Thoughts on data security (Reprint)
猜你喜欢

浅谈专项测试之弱网络测试
![Buuctf-[bjdctf2020]zjctf, but so (xiaoyute detailed explanation)](/img/c9/56fba6054c91f090de31463a8b4705.jpg)
Buuctf-[bjdctf2020]zjctf, but so (xiaoyute detailed explanation)

Clock in during winter vacation

On weak network test of special test

SQLMAP使用教程(三)实战技巧二

Database - current read and snapshot read

功能安全之故障(fault),错误(error),失效(failure)

Database isolation level

What are the test sites for tunnel engineering?

全程实现单点登录功能和请求被取消报错“cancelToken“ of undefined的解决方法
随机推荐
GTSAM中李群的运用
黑猫带你学UFS协议第8篇:UFS初始化详解(Boot Operation)
Application of Lie group in gtsam
A complete collection of necessary learning websites for office programmers
【Postman】测试(Tests)脚本编写和断言详解
F - true liars (category and search set +dp)
[postman] dynamic variable (also known as mock function)
Company video accelerated playback
[web security] nodejs prototype chain pollution analysis
[eolink] PC client installation
[postman] collections - run the imported data file of the configuration
黑猫带你学UFS协议第4篇:UFS协议栈详解
RestTemplate、Feign实现Token传递
Overview of three core areas of Mathematics: algebra
【Tera Term】黑猫带你学TTL脚本——嵌入式开发中串口自动化神技能
Summary of anomaly detection methods
把el-tree选中的数组转换为数组对象
Resttemplate and feign realize token transmission
Fault, error, failure of functional safety
进程和线程的理解