当前位置:网站首页>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;
}
}
边栏推荐
- 【面试:并发篇38:多线程:线程池】ThreadPoolExecutor类的基本概念
- 单点登录 思维导图
- 2022.07.20_每日一题
- Financial leasing business
- 【微服务】 微服务学习笔记二:Eureka注册中心的介绍及搭建
- 【 TA - frost Wolf _may - "one hundred plan" 】 art 2.3 hard surface
- leetcode 406. Queue Reconstruction by Height 根据身高重建队列(中等)
- 【并发编程】ReentrantLock的lock()方法源码分析
- 2022.07.12_Daily Question
- 线程中断方法
猜你喜欢
One of the small practical projects - food alliance ordering system
2022.07.18 _ a day
把 VS Code 当游戏机
Redux state management
金融租赁业务
2022.07.13_每日一题
Postgresql source code learning (34) - transaction log ⑩ - full page write mechanism
Log4net 思维导图
【网络攻防】常见的网络攻防技术——黑客攻防(通俗易懂版)
2. (1) Chained storage of stack, operation of chain stack (illustration, comment, code)
随机推荐
小实战项目之——吃货联盟订餐系统
知识、创新、回报。
【Star项目】小帽飞机大战(八)
事务的四大特性
2022.07.26_Daily Question
Conditional statements of shell (test, if, case)
剑指offer(一)
Introduction and self-order of bcos
2022.07.26_每日一题
Postgresql source code learning (34) - transaction log ⑩ - full page write mechanism
Foreign trade website optimization - foreign trade website optimization tutorial - foreign trade website optimization software
解决安装 Bun 之后出现 zsh compinit: insecure directories, run compaudit for list. Ignore insecure directorie
Yu Mr Series 】 【 2022 July 022 - Go Go teaching course of container in the dictionary
[PSQL] SQL基础教程读书笔记(Chapter1-4)
postgresql源码学习(34)—— 事务日志⑩ - 全页写机制
什么是半波整流器?半波整流器的使用方法
【Go语言入门】一文搞懂Go语言的最新依赖管理:go mod的使用
LeetCode brush # 376 # Medium - swing sequence
R——避免使用 col=0
nohup原理