当前位置:网站首页>Simulation volume leetcode [general] 1314 Matrix area and
Simulation volume leetcode [general] 1314 Matrix area and
2022-07-06 06:18:00 【Encounter simulation volume】
1314. Matrix regions and
To give you one m x n Matrix mat And an integer k , Please return a matrix answer , Each of them answer[i][j] Are all elements that meet the following conditions mat[r][c] And :
i - k <= r <= i + k,
j - k <= c <= j + k And
(r, c) In the matrix .
Example 1:
Input :mat = [[1,2,3],[4,5,6],[7,8,9]], k = 1
Output :[[12,21,16],[27,45,33],[24,39,28]]
Example 2:
Input :mat = [[1,2,3],[4,5,6],[7,8,9]], k = 2
Output :[[45,45,45],[45,45,45],[45,45,45]]
Tips :
m == mat.length
n == mat[i].length
1 <= m, n, k <= 100
1 <= mat[i][j] <= 100
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/matrix-block-sum
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Code :
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]]:
""" But I don't know why the result is wrong , In particular, the convolution kernel exceeds mat When """
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')
remarks :
GitHub:https://github.com/monijuan/leetcode_python
CSDN Summary : Simulation volume Leetcode Summary of questions _ Paper blog -CSDN Blog
You can add QQ Group communication :1092754609
leetcode_python.utils See the description on the summary page for details
First brush questions , Then generated by script blog, If there is any mistake, please leave a message , I see it will be revised ! thank you !
边栏推荐
- 异常检测方法总结
- MySQL之数据类型
- 浅谈专项测试之弱网络测试
- Eigen稀疏矩阵操作
- IP day 16 VLAN MPLS configuration
- Caused by:org.gradle.api.internal.plugins . PluginApplicationException: Failed to apply plugin
- 模拟卷Leetcode【普通】1414. 和为 K 的最少斐波那契数字数目
- Fault, error, failure of functional safety
- 调用链监控Zipkin、sleuth搭建与整合
- 【Postman】Collections-运行配置之导入数据文件
猜你喜欢
keil MDK中删除添加到watch1中的变量
Basic knowledge of error
【C语言】字符串左旋
Idea new UI usage
G - Supermarket
Construction and integration of Zipkin and sleuth for call chain monitoring
Digital triangle model acwing 1015 Picking flowers
[wechat applet] build a development tool environment
Overview of three core areas of Mathematics: algebra
MySQL之基础知识
随机推荐
模拟卷Leetcode【普通】1219. 黄金矿工
【Postman】测试(Tests)脚本编写和断言详解
The latest 2022 review of "graph classification research"
Manhattan distance sum - print diamond
【微信小程序】搭建开发工具环境
Database isolation level
Seven imperceptible truths in software testing
Isam2 operation process
On weak network test of special test
数据库-当前读与快照读
还在为如何编写Web自动化测试用例而烦恼嘛?资深测试工程师手把手教你Selenium 测试用例编写
Eigen sparse matrix operation
Left matching principle of joint index
数据库隔离级别
10m25dcf484c8g (FPGA) amy-6m-0002 BGA GPS module
Nodejs realizes the third-party login of Weibo
模拟卷Leetcode【普通】1143. 最长公共子序列
Web界面元素的测试
把el-tree选中的数组转换为数组对象
[wechat applet] build a development tool environment