当前位置:网站首页>LeetCode:1260. 二维网格迁移【一维展开+拼接】
LeetCode:1260. 二维网格迁移【一维展开+拼接】
2022-07-21 02:08:00 【星空皓月】
题目描述
给你一个 m 行 n 列的二维网格 grid 和一个整数 k。你需要将 grid 迁移 k 次。
每次「迁移」操作将会引发下述活动:
- 位于 grid[i][j] 的元素将会移动到 grid[i][j + 1]。
- 位于 grid[i][n - 1] 的元素将会移动到 grid[i + 1][0]。
- 位于 grid[m - 1][n - 1] 的元素将会移动到 grid[0][0]。
请你返回 k 次迁移操作后最终得到的 二维网格。
输入与输出
输入:grid = [[1,2,3],[4,5,6],[7,8,9]], k = 1
输出:[[9,1,2],[3,4,5],[6,7,8]]
提示
- m == grid.length
- n == grid[i].length
- 1 <= m <= 50
- 1 <= n <= 50
- -1000 <= grid[i][j] <= 1000
- 0 <= k <= 100
思路
将二维展开成一维,其实就是将一维数组向后移动k,然后再拼接成二维。
AC代码
class Solution {
public:
vector<vector<int>> shiftGrid(vector<vector<int>>& grid, int k) {
int n = grid.size();
int m = grid[0].size();
vector<int> temp;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
temp.push_back(grid[i][j]);
}
}
int len = n * m;
int t = k;
int pos = 0;
t %= len;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (t > 0) {
grid[i][j] = temp[len - t];
t--;
} else {
grid[i][j] = temp[pos];
pos++;
}
}
}
return grid;
}
};
时间复杂度和空间复杂度都为O(m*n)
边栏推荐
- Solution to the problem of interval variance of valley p5142
- C language -- 24 Gobang
- How to extract the specified column from the database of multiple imputation as a new variable
- Getting started with ctfshow web (code audit)
- CANoe面板中的控件:Switch/Indicator
- Metal organic framework mil-100 (CR) and mil-101 (CR) Supported Phosphotungstic Acid | zirconium based metal organic framework [email pr
- NH2-MIL-125 (Ti)/TiO2复合纳米纤维|镧系金属有机骨架([email protected])|mof有机骨架材料
- C language to find the greatest common divisor and the least common multiple of two numbers
- 力扣(LeetCode)201. 数字范围按位与(2022.07.20)
- It is not just products that keep pace with the times. Look at FAW Toyota's confidence in "Second Entrepreneurship" from Carola Ruifang
猜你喜欢
[email protected])|mof有机骨架材料"/>
NH2-MIL-125 (Ti)/TiO2复合纳米纤维|镧系金属有机骨架([email protected])|mof有机骨架材料
数字孪生落地高铁桥梁可视化解决方案
Getting started with ctfshow web (file upload)
Php-cgi Remote Code Execution Vulnerability (cve-2012-1823)
Table auto paging
零基础学习CANoe Panel(1)—— 新建 Panel
【04】穿越功耗墙,我们该从哪些方面提升“性能”?
[email protected]催化剂([e"/>
Ag掺杂改性金属有机骨架多孔材料MIL-101|核壳结构的[email protected]催化剂([e
Skiasharp's WPF self drawn bouncing ball (case version)
Ethylenediamine modified metal organic framework material mil-101 (CR) | functional mofs/ polymer composites | zif-8 / tetradecyl hexadecyl acrylate copolymer (zif-8/p (tda--hda)
随机推荐
从新零售到社区团购,这中间发生了多少变化?
【翻译】开发人员的技术写作
Getting started with ctfshow web (code audit)
[notes for question brushing] factorial mantissa
【04】穿越功耗墙,我们该从哪些方面提升“性能”?
【翻译】用Kubernetes + Helm + Flux提高稳定性和可靠性!
High performance server model summary
洛谷P5142 区间方差 题解
The domestic epidemic is repeated. How can offline physical stores be transformed to break through the dilemma?
工业4.0数字孪生下的应用案例
技术小百科 | 云原生篇
Ag掺杂改性金属有机骨架多孔材料MIL-101|核壳结构的[email protected]催化剂([e
AG doped metal organic framework porous material mil-101 | core shell structure [email protected] Catalyst (NH2 UIO-
金属有机框架MIL-100(Cr)和MIL-101(Cr)负载磷钨酸|锆基金属有机骨架[email protected]多孔陶瓷复合材料(齐岳mof材料)
Php-cgi Remote Code Execution Vulnerability (cve-2012-1823)
ctfshow web入门(代码审计)
解决Kettle8.2版本錶輸入-Excel輸出時,時間字段空白
College student community management system based on SSM framework
It is not just products that keep pace with the times. Look at FAW Toyota's confidence in "Second Entrepreneurship" from Carola Ruifang
事件对象学习