当前位置:网站首页>【LeetCode】1260. 2D mesh migration
【LeetCode】1260. 2D mesh migration
2022-07-21 19:10:00 【pass night】
subject
1260. Two dimensional grid migration
To give you one m
That's ok n
Two dimensional grid of columns grid
And an integer k
. You need to grid
transfer k
Time .
Every time 「 transfer 」 The operation will trigger the following activities :
- be located
grid[i][j]
The element will be moved togrid[i][j + 1]
. - be located
grid[i][n - 1]
The element will be moved togrid[i + 1][0]
. - be located
grid[m - 1][n - 1]
The element will be moved togrid[0][0]
.
Please return k
The final result after the migration operation Two dimensional meshes .
Example 1:
Input :grid = [[1,2,3],[4,5,6],[7,8,9]], k = 1
Output :[[9,1,2],[3,4,5],[6,7,8]]
Example 2:
Input :grid = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4
Output :[[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]
Example 3:
Input :grid = [[1,2,3],[4,5,6],[7,8,9]], k = 9
Output :[[1,2,3],[4,5,6],[7,8,9]]
Tips :
m == grid.length
n == grid[i].length
1 <= m <= 50
1 <= n <= 50
-1000 <= grid[i][j] <= 1000
0 <= k <= 100
Ideas
- Observe the movement , It is not difficult to find the law of movement, which is equivalent to each number moving forward after horizontal expansion k A unit of
Code
class Solution:
def shiftGrid(self, grid: List[List[int]], k: int) -> List[List[int]]:
m,n = len(grid), len(grid[0])
ret = [[0]*n for _ in range(m)]
for i in range(m):
for j in range(n):
newIndex = (i*n+j+k) % (m*n)
ret[newIndex//n][newIndex%n] = grid[i][j]
return ret
Complexity
- Time complexity : O ( m n ) O(mn) O(mn)
- Spatial complexity : O ( 1 ) O(1) O(1)
边栏推荐
- Part I - Fundamentals of C language_ 9. Composite type (custom type)
- 软件测试面试题:黑盒测试的测试用例常见设计方法都有哪些?请分别以具体的例子来说明这些方法在测试用例设计工作中的应用。
- 软件测试面试题:测试计划工作的目的是什么?测试计划文档的内容应该包括什么?其中哪些是最重要的?
- [QNX hypervisor 2.2 user manual]8.6 interrupt
- swift和OC对比,Swift的核心思想:面向协议编程
- 软件测试面试题:你自认为测试的优势在哪里?
- Part I - Fundamentals of C language_ 10. Document operation
- 红枣科技何亦凡:NFT基础设施要具备哪些特性?
- mapbox-gl开发教程(十三):加载三维面图层(白模)
- Session 和 Cookies 有什么区别?
猜你喜欢
How to realize copyright protection of Digital Collections
kubernetes的这几种存储卷,别再傻傻分不清了
过滤器 监听器
Obtain the screenshot of the front panel through programming in LabVIEW
教程篇(7.0) 04. FortiClient 部署 * FortiClient EMS * Fortinet 网络安全专家 NSE 5
kubernetes的这几种存储卷,别再傻傻分不清了
SuperMap iClient for OpenLayers图层组控制实现方法
【信息检索】信息检索系统实现
Translation and interpretation of the paper: anytime bottom up rule learning for knowledge graph completion [anyburr]
牛客网刷题篇
随机推荐
Session 和 Cookies 有什么区别?
解决跨域问题的8种方案(最新最全)
Streamlit 数据科学必备工具
Rk3128 speaker SPK and headset HP sound size debugging
奈雪的茶布局元宇宙 传统品牌如何转战元宇宙营销
Pytorch环境搭建
V853 development board hardware data - risc-v core e907 user manual
LogMAP: logic based and scalable ontology matching
牛客网刷题篇
2022软件测试技能 Jmeter+Ant+Jenkins持续集成并生成测试报告教程
论文翻译解读:Logmap:Logic-based and scalable ontology matching
C language operators
The new red envelope cover platform can build the source code of the independent background of the sub station
Part I - Fundamentals of C language_ 9. Composite type (custom type)
JMeter学习笔记003-JMeter之CSV参数化
C language -- the use and Simulation of library function qsort
Powermock actual combat
时间序列数据:预测
PostgreSQL source code (64) query execution - sub module executor (2) data structure and execution process before execution
FTP服務配置