当前位置:网站首页>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.length
n == grid[i].length
1 <= m <= 50
1 <= n <= 50
-1000 <= grid[i][j] <= 1000
0 <= 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;
}
}
边栏推荐
- 事务的传播机制
- 完美指南|如何使用 ODBC 进行无代理 Oracle 数据库监控?
- Difficulty comparison between high concurrency and multithreading (easy to confuse)
- 2.(1)栈的链式存储、链栈的操作(图解、注释、代码)
- Analysis of the principle and implementation of waterfall flow layout
- 2. (1) Chained storage of stack, operation of chain stack (illustration, comment, code)
- Foreign trade website optimization - foreign trade website optimization tutorial - foreign trade website optimization software
- postgresql源码学习(33)—— 事务日志⑨ - 从insert记录看日志写入整体流程
- 【Go语言入门教程】Go语言简介
- 2022.07.29_Daily Question
猜你喜欢
Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)
DAY18: XSS vulnerability
【Go语言入门教程】Go语言简介
文件 - 04 下载文件: 根据文件下载链接下载文件
Zabbix6.2 Surprise Release!Especially optimize the performance of medium and large environment deployment!
【微服务】 微服务学习笔记二:Eureka注册中心的介绍及搭建
零样本学习&Domain-aware Visual Bias Eliminating for Generalized Zero-Shot Learning
DAY18:Xss 靶场通关手册
完美指南|如何使用 ODBC 进行无代理 Oracle 数据库监控?
Zotero | Zotero translator plugin update | Solve the problem that Baidu academic literature cannot be obtained
随机推荐
2022.07.12_Daily Question
【微服务】Nacos集群搭建以及加载文件配置
文件 - 04 下载文件: 根据文件下载链接下载文件
03-SDRAM: Write operation (burst)
Yu Mr Series 】 【 2022 July 022 - Go Go teaching course of container in the dictionary
DAY18:Xss 靶场通关手册
Install the gstreamer development dependency library to the project sysroot directory
PCB抄板
2022.07.15_Daily Question
‘vite‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
Log4net 思维导图
2022.07.18_每日一题
2022.07.18 _ a day
Kubernetes调度
【 TA - frost Wolf _may - "one hundred plan" 】 art 2.3 hard surface
tidyverse笔记——dplyr包
Zabbix6.2 Surprise Release!Especially optimize the performance of medium and large environment deployment!
LeetCode:952. 按公因数计算最大组件大小【欧拉筛 + 并查集】
【Go报错】go go.mod file not found in current directory or any parent directory 错误解决
MySQL安装到最后一步 write configuration file 失败 怎么办?及后安装步骤