当前位置:网站首页>2022.07.20_每日一题
2022.07.20_每日一题
2022-07-31 06:07:00 【诺.い】
1260. 二维网格迁移
题目描述
给你一个 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]]
提示:
m == grid.lengthn == grid[i].length1 <= m <= 501 <= n <= 50-1000 <= grid[i][j] <= 10000 <= k <= 100
- 数组
- 矩阵
- 模拟
coding
1. 新建数组,直接平移,然后存入结果
class Solution {
public List<List<Integer>> shiftGrid(int[][] grid, int k) {
List<List<Integer>> res = new ArrayList<>();
int row = grid.length;
int col = grid[0].length;
// 用于存储移动后的数据
Integer[][] arr = new Integer[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
// 原本 i, j 位置上的数在移动后数组位置发生了变化
// 行 i => (i + (j + k) / col) % row
// 列 j => (j + k) % col
//【PS】: 行的话应注意是否换行
arr[(i + (j + k) / col) % row][(j + k) % col] = grid[i][j];
}
}
// for (Integer[] rows : arr) {
// res.add(Arrays.asList(rows));
// }
Arrays.stream(arr).forEach(i -> res.add(Arrays.asList(i)));
return res;
}
}
2. 二维数组转为一维数组,存入结果集
class Solution {
public List<List<Integer>> shiftGrid(int[][] grid, int k) {
List<List<Integer>> res = new ArrayList<>();
int row = grid.length;
int col = grid[0].length;
int len = row * col;
int[] arr = new int[len];
int index = 0;
// 二维转移动后的一维
for (int[] rows : grid) {
for (int num : rows) {
arr[((index ++) + k) % len] = num;
}
}
index = 0;
for (int i = 0; i < row; i++) {
List<Integer> list = new ArrayList<>();
for (int j = 0; j < col; j++) {
list.add(arr[index++]);
}
res.add(list);
}
return res;
}
}
边栏推荐
- CHI论文阅读(1)EmoGlass: an End-to-End AI-Enabled Wearable Platform for Enhancing Self-Awareness of Emoti
- 简单谈谈Feign
- 数据库原理作业2 — JMU
- Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)
- codec2 BlockPool:unreadable libraries
- 《白帽子说Web安全》思维导图
- 在 ASP.NET Core 应用程序启动时运行代码的 3 种方法
- 安装gstreamer开发依赖库到项目sysroot目录
- 事务的传播机制
- 【科普向】5G核心网架构和关键技术
猜你喜欢

树状数组(单点修改区间查询和区间修改单点查询)

Automatic translation software - batch batch automatic translation software recommendation

Run the NPM will pop up to ask "how are you going to open this file?"

科普 | “大姨太”ETH 和 “小姨太”ETC的爱恨情仇

毫米波技术基础

iOS大厂面试查漏补缺

Project exercise - memorandum (add, delete, modify, check)

测试 思维导图

数据库原理作业2 — JMU

【微服务】(十六)—— 分布式事务Seata
随机推荐
文件 - 07 删除文件: 根据fileIds批量删除文件及文件信息
第三方库-store
LeetCode刷题——摆动序列#376#Medium
数据库概论 - MySQL的简单介绍
DDL+DML+DQL
Project exercise - memorandum (add, delete, modify, check)
【第四章】详解Feign的实现原理
解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?
解决安装 Bun 之后出现 zsh compinit: insecure directories, run compaudit for list. Ignore insecure directorie
事务的传播机制
Zotero | Zotero translator插件更新 | 解决百度学术文献无法获取问题
PCB抄板
Chapter 17: go back to find the entrance to the specified traverse, "ma bu" or horse stance just look greedy, no back to search traversal, "ma bu" or horse stance just look recursive search NXM board
Conditional statements of shell (test, if, case)
MySQL笔记下
拉格朗日插值及其应用
4-1-7 二叉树及其遍历 家谱处理 (30 分)
【面试:并发篇37:多线程:线程池】自定义线程池
MySql的安装配置超详细教程与简单的建库建表方法
CHI论文阅读(1)EmoGlass: an End-to-End AI-Enabled Wearable Platform for Enhancing Self-Awareness of Emoti