当前位置:网站首页>2022.07.20_Daily Question
2022.07.20_Daily Question
2022-07-31 07:40:00 【No. い】
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. 新建数组,直接平移,then store the result
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;
// Used to store moved data
Integer[][] arr = new Integer[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
// 原本 i, j The number at the position changes the array position after the move
// 行 i => (i + (j + k) / col) % row
// 列 j => (j + k) % col
//【PS】: If you line it, you should pay attention to whether to wrap the line
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;
// 2D transformed into 1D after moving
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棋盘带障碍马步遍历
- Leetcode952. 按公因数计算最大组件大小
- ‘vite‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
- 2022.07.12_每日一题
- 基于交替迭代法的交直流混合系统潮流计算matlab程序iEEE9节点系统算例
- 2022.7.29 Array
- 服务器和客户端信息的获取
- 解决安装 Bun 之后出现 zsh compinit: insecure directories, run compaudit for list. Ignore insecure directorie
- 【愚公系列】2022年07月 Go教学课程 022-Go容器之字典
- 2022.07.14_每日一题
猜你喜欢

LeetCode:952. 按公因数计算最大组件大小【欧拉筛 + 并查集】

基于交替迭代法的交直流混合系统潮流计算matlab程序iEEE9节点系统算例

把 VS Code 当游戏机

The Perfect Guide|How to use ODBC for Agentless Oracle Database Monitoring?

Leetcode952. 按公因数计算最大组件大小

Redux state management

iOS大厂面试查漏补缺

How to set the computer password?How to add "safety lock" to your computer

2022.07.24_每日一题

讲解实例+详细介绍@Resource与@Autowired注解的区别(全网最全)
随机推荐
电脑开机密码怎么设置?如何给你的电脑加上“安全锁”
mysql的建表语句_三种常用的MySQL建表语句
[PSQL] 复杂查询
Chapter 16: Constructing the Magic Square for Prime Numbers of Order n(5,7)
04-SDRAM:读操作(突发)
leetcode 406. Queue Reconstruction by Height
Postgresql source code learning (33) - transaction log ⑨ - see the overall process of log writing from the insert record
postgresql源码学习(34)—— 事务日志⑩ - 全页写机制
2022.07.13_每日一题
事务的四大特性
基于LSTM的诗词生成
2022.07.13 _ a day
gstreamer's caps event and new_segment event
多进程全局变量失效、变量共享问题
【面试:并发篇37:多线程:线程池】自定义线程池
Jobject 使用
Postgresql source code learning (34) - transaction log ⑩ - full page write mechanism
2022.07.15_Daily Question
Kubernetes scheduling
R——避免使用 col=0