当前位置:网站首页>1260. 二维网格迁移 : 简单构造模拟题
1260. 二维网格迁移 : 简单构造模拟题
2022-07-20 12:04:00 【宫水三叶的刷题日记】
题目描述
这是 LeetCode 上的 1260. 二维网格迁移 ,难度为 简单。
Tag : 「模拟」、「构造」
给你一个 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
次迁移操作后最终得到的 二维网格
示例 1:
输入:grid = [[1,2,3],[4,5,6],[7,8,9]], k = 1
输出:[[9,1,2],[3,4,5],[6,7,8]]
示例 2:
输入:grid = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4
输出:[[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]
示例 3:
输入:grid = [[1,2,3],[4,5,6],[7,8,9]], k = 9
输出:[[1,2,3],[4,5,6],[7,8,9]]
提示:
模拟
为了方便,我们令 grid
为 g
,令 n
和 m
分别为 g
的行数和列数。
由于迁移过程存在明显规律性,因此我们可以直接 算得每一列最终所在的列下标tcol = (i + k) % m
(其中 i
为原本的列下标),同时 算得当前列的首行元素在新列中的行下标trow = ((i + k) / m) % n
,之后就是简单的遍历赋值操作。
Java 代码:
class Solution {
public List<List<Integer>> shiftGrid(int[][] g, int k) {
int n = g.length, m = g[0].length;
int[][] mat = new int[n][m];
for (int i = 0; i < m; i++) {
int tcol = (i + k) % m, trow = ((i + k) / m) % n, idx = 0;
while (idx != n) {
mat[trow++][tcol] = g[idx++][i];
if (trow == n) trow = 0;
}
}
List<List<Integer>> ans = new ArrayList<>();
for (int i = 0; i < n; i++) {
List<Integer> alist = new ArrayList<>();
for (int j = 0; j < m; j++) alist.add(mat[i][j]);
ans.add(alist);
}
return ans;
}
}
TypeScript 代码:
function shiftGrid(g: number[][], k: number): number[][] {
const n = g.length, m = g[0].length
const ans: number[][] = new Array<Array<number>>()
for (let i = 0; i < n; i++) ans[i] = new Array<number>(m).fill(0)
for (let i = 0; i < m; i++) {
let tcol = (i + k) % m, trow = Math.floor(((i + k) / m)) % n, idx = 0
while (idx != n) {
ans[trow++][tcol] = g[idx++][i]
if (trow == n) trow = 0
}
}
return ans
};
时间复杂度: 空间复杂度:
最后
这是我们「刷穿 LeetCode」系列文章的第 No.1260
篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
本文由 mdnice 多平台发布
边栏推荐
- Google Earth Engine——MERRA-2 M2T1NXAER:1980-2022年气溶胶逐日数据集
- Annual weight! Huawei cloud 2021 application construction technology practice collection, 400 pages in seven fields + cloud development classic | Yunxiang library No.02 recommendation (with free downl
- Nextcloud个人云存储绝佳选择:一键自动安装方法和云盘使用体验
- OCR/STR生僻字数据训练 | PaddleOCR的Fine-tune常见问题汇总(3)
- 云生态大会,随“峰”而来!
- 【暑期每日一题】洛谷 P1605 迷宫
- Five ways of accelerating digital transformation with low code
- 【暑期每日一题】洛谷 P6850 NOI
- 接口自动化测试---pytest测试用例设计
- PMP考试重点难点汇总
猜你喜欢
接口自动化测试---pytest测试用例设计
上海文旅局局长:安全是文旅业的生命线,正抢抓元宇宙新赛道
Data structure of MySQL index
Redis學習筆記(1)——B站動力節點
最右×微帧,高质量的HEIF图片编码压缩技术
PG operation and maintenance -- common management commands
C#/VB.NET在 Word 中插入水印
Open source remote desktop software rustdesk
Interface automation test -- pytest test test case design
OCR/STR生僻字数据集生成 | PaddleOCR的垂类Fine-tune(1)
随机推荐
Data structure of MySQL index
R语言epiDisplay包的kap函数计算配对列联表的计算一致性的比例以及Kappa统计量的值(总一致性、期望一致性)、使用xtabs函数生成二维列联表、使用wttable参数设置权重表参数为w2
自动推理的逻辑05--谓词演算
【单片机仿真项目】数码管动态显示
ArcGIS Api For Flex 动态画点和线
Lecture 30 linear algebra Lecture 6 quadratic form
[IOT] Product Manager: the underlying logic of human insight
MapStruct - Consider defining a bean of type in your configuration.
2022.7.20 线性表
LeetCode#101 对称二叉树
2020华为云社区年度技术精选合集,700页+免费下载! | 云享·书库 No.01 期推荐(附免费下载)
KusionStack 开源|Kusion 模型库和工具链的探索实践
Dall-E2和Imgen有什么区别?Reddit热帖盘点效果差异
1260. 二维网格迁移
MSYQL multiple or, and,
The drawing principle and application of Bao Jiao Bao Hui Bezier curve
uva11100
Multi level cache solution
QT之隐藏控件
阿里云发布《升舱-数据仓库升级交付标准化》白皮书