当前位置:网站首页>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.length
n == grid[i].length
1 <= m <= 50
1 <= n <= 50
-1000 <= grid[i][j] <= 1000
0 <= 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;
}
}
边栏推荐
- 第十七章:回溯探求指定入口的马步遍历,贪心无回溯探求马步遍历,递归探求nxm棋盘带障碍马步遍历
- Analysis of the implementation principle and detailed knowledge of v-model syntactic sugar and how to make the components you develop support v-model
- 从入门到一位合格的爬虫师,这几点很重要
- Moment.js common methods
- 多进程全局变量失效、变量共享问题
- Postgresql source code learning (33) - transaction log ⑨ - see the overall process of log writing from the insert record
- Difficulty comparison between high concurrency and multithreading (easy to confuse)
- 新瓶陈酒 --- 矩阵快速幂
- 【云原生】-Docker容器迁移Oracle到MySQL
- DirectExchange switch simple introduction demo
猜你喜欢
Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)
DirectExchange switch simple introduction demo
外贸网站优化-外贸网站优化教程-外贸网站优化软件
How to use repeating-linear-gradient
2.(1)栈的链式存储、链栈的操作(图解、注释、代码)
【微服务】(十六)—— 分布式事务Seata
One of the small practical projects - food alliance ordering system
浅层了解欧拉函数
03-SDRAM: Write operation (burst)
SCI写作指南
随机推荐
Kubernetes scheduling
快速傅里叶变换(FFT)
mysql的下载及安装使用
How to use repeating-linear-gradient
PCB抄板
Bulk free text translation
【云原生】-Docker安装部署分布式数据库 OceanBase
Some derivation formulas for machine learning backpropagation
第十六章:构建n(5,7)阶素数幻方
数据库原理作业3 — JMU
浅层了解欧拉函数
Postgresql source code learning (33) - transaction log ⑨ - see the overall process of log writing from the insert record
测试 思维导图
关于求反三角函数的三角函数值
【Go报错】go go.mod file not found in current directory or any parent directory 错误解决
毫米波技术基础
QFileInfo常规方法
【云原生】-Docker容器迁移Oracle到MySQL
那些破釜沉舟入局Web3.0的互联网精英都怎么样了?
codec2 BlockPool:unreadable libraries