当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢
高并发与多线程之间的难点对比(容易混淆)
Run the NPM will pop up to ask "how are you going to open this file?"
解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?
金融租赁业务
【Go语言入门教程】Go语言简介
Core Tower Electronics won the championship in the Wuhu Division of the 11th China Innovation and Entrepreneurship Competition
[PSQL] SQL基础教程读书笔记(Chapter1-4)
【Go报错】go go.mod file not found in current directory or any parent directory 错误解决
文件 - 03 下载文件:根据文件id获取下载链接
小实战项目之——吃货联盟订餐系统
随机推荐
Conditional statements of shell (test, if, case)
04-SDRAM:读操作(突发)
postgresql源码学习(33)—— 事务日志⑨ - 从insert记录看日志写入整体流程
SCI写作指南
解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?
Zabbix6.2 Surprise Release!Especially optimize the performance of medium and large environment deployment!
2022.07.29_Daily Question
The Ballad of Lushan Sends Lu's Servant to the Void Boat
我开发了一个利用 Bun 执行 .ts / .js 文件的 VS Code 插件
在 ASP.NET Core 应用程序启动时运行代码的 3 种方法
从入门到一位合格的爬虫师,这几点很重要
DAY18: XSS vulnerability
从 Google 离职,前Go 语言负责人跳槽小公司
DAY18:Xss 靶场通关手册
【微服务】Nacos集群搭建以及加载文件配置
知识、创新、回报。
Obtaining server and client information
链表实现及任务调度
Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)
【愚公系列】2022年07月 Go教学课程 022-Go容器之字典