当前位置:网站首页>Leetcode-1260. 2D mesh migration
Leetcode-1260. 2D mesh migration
2022-06-12 05:56:00 【Taoist scholar】
link
1260. Two dimensional grid migration
subject
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 to grid[i][j + 1].
- be located grid[i][n - 1] The element will be moved to grid[i + 1][0].
- be located grid[m - 1][n - 1] The element will be moved to grid[0][0].
Please return k The final result after the migration operation Two dimensional meshes .
Example

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]]
explain
- m == grid.length
- n == grid[i].length
- 1 <= m <= 50
- 1 <= n <= 50
- -1000 <= grid[i][j] <= 1000
- 0 <= k <= 100
Ideas
We can find a rule through questions and examples , It is equivalent to expanding a two-dimensional array into a one-dimensional array , Move all elements back one bit , And the last element moves to the starting position , As example 1 [1 2 3 4 5 6 7 8 9]——>[9 1 2 3 4 5 6 7 8]
C++ Code
class Solution {
public:
vector<vector<int>> shiftGrid(vector<vector<int>>& grid, int k) {
int m = grid.size();
int n = grid[0].size();
for (k; k > 0; k--)
{
int previous = grid[m-1][n-1];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
swap(grid[i][j], previous);
}
return grid;
}
};
边栏推荐
猜你喜欢

TCP and UDP introduction

数据集成框架SeaTunnel学习笔记
![Leetcode buckle -10 Regular expression matching analysis [recursion and dynamic programming]](/img/25/b3c475e2b03c39b7c576b6d01f9d56.jpg)
Leetcode buckle -10 Regular expression matching analysis [recursion and dynamic programming]

Towards End-to-End Lane Detection: an Instance SegmentationApproach

Understanding of distributed transactions

IO stream introduction

Redis persistence

Guns框架多数据源配置,不修改配置文件

Filter的注解配置

Simple introduction to key Wizard
随机推荐
Redis persistence
China's alternative sports equipment market trend report, technology dynamic innovation and market forecast
Date ()
Makefile文件编写快速掌握
Identification of campus green plants based on tensorflow
User login 【 I 】
Rtmp/rtsp/hls public network real available test address
nRF52832自定义服务与特性
Multiple ways 99.9% to solve the problem of garbled code after copying text from PDF
Recursive implementation of exponential, permutation and combination enumerations
Conversion of Halcon 3D depth map to 3D image
Json-c common APIs
Unable to access this account. You may need to update your password or grant the account permission to synchronize to this device. Tencent corporate email
Memory model, reference and function supplement of program
[untitled]
[road of system analyst] collection of wrong topics in software engineering chapters
Halcon 3D 1 Reading 3D data
Go interface implementation principle [advanced level]
Chapter 7 - pointer learning
基于LFFD模型目标检测自动标注生成xml文件