当前位置:网站首页>[daily training] 1260 2D mesh migration
[daily training] 1260 2D mesh migration
2022-07-22 01:14:00 【Puppet__】
subject
To give you one m That's ok n Two dimensional grid of columns grid And an integer k. You need to grid transfer k Time .
Every time 「 transfer 」 The operation will trigger the following activities :
be located grid[i][j] The element will be moved to grid[i][j + 1].
be located grid[i][n - 1] The element will be moved to grid[i + 1][0].
be located grid[m - 1][n - 1] The element will be moved to grid[0][0].
Please return k The final result after the migration operation Two dimensional meshes .
Example 1:
Input :grid = [[1,2,3],[4,5,6],[7,8,9]], k = 1
Output :[[9,1,2],[3,4,5],[6,7,8]]
Example 2:
Input :grid = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4
Output :[[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]
Example 3:
Input :grid = [[1,2,3],[4,5,6],[7,8,9]], k = 9
Output :[[1,2,3],[4,5,6],[7,8,9]]
Tips :
m== grid.length
n == grid[i].length
1 <= m <= 50
1 <= n <= 50
-1000 <= grid[i][j] <= 1000
0 <= k <= 100
Code
package dayLeetCode;
import java.util.ArrayList;
import java.util.List;
public class dayleetcode1260 {
// Convert to one dimension
public List<List<Integer>> shiftGrid(int[][] grid, int k) {
int m = grid.length, n = grid[0].length;
List<List<Integer>> ansList = new ArrayList<>();
for (int i = 0; i < m; i++){
List<Integer> tmpList = new ArrayList<>();
for (int j = 0; j < n; j++){
tmpList.add(grid[i][j]);
}
ansList.add(tmpList);
}
// It will cycle to the right once every time
for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++){
// Calculation k Coordinates in one-dimensional state after the second movement
int index = (i * n + j + k) % (m * n);
// The position of the current value changes to [index/n, index %n]
ansList.get(index / n).set(index % n, grid[i][j]);
}
}
return ansList;
}
public static void main(String[] args) {
dayleetcode1260 obj = new dayleetcode1260();
for (List<Integer> list : obj.shiftGrid(new int[][]{
{
1, 2, 3}, {
4, 5, 6}, {
7, 8, 9}}, 1)) {
for (int num : list){
System.out.print(num);
}
System.out.println();
}
}
}
边栏推荐
- Ultra high precision UWB is not expensive -- detailed explanation of UWB positioning base station cost
- C language 10 entry questions set
- Halcon and opencv finally chose the latter
- Sword finger offer_ Knowledge transfer ability
- OpenShift Security (17) - 将 OpenShift Compliance Operator 的合规扫描集成到 RHACS
- Leetcode [剑指 Offer II 068. 查找插入位置
- TCP/IP协议栈Lwip的设计与实现:之七
- Difference between two lists
- The Sandbox 联手 Agoria,共同打造 Agoriaverse
- 整数二分、浮点二分以及STL中的二分函数(lower_bound(),upper_bound())
猜你喜欢
websocket 传输文件
20220719给AIO-3568J适配OpenHarmony-v3.1-beta
What is the new generation of enterprise IT architecture? Cloud primordial? Low code?
探寻机器人创客教育中的趣味
Sword finger offer_ Knowledge transfer ability
解读符合新时代主流的创客教育模式
OpenShift 4 - 安装 ODF 并部署红帽 Quay (3 Worker)
[终端_1]-Xshell 5 最火的终端软件!
【网络安全】面试中常见问题--sql注入篇
牛客刷题 01——KiKi去重整数并排序(C语言)
随机推荐
visual studio引用外部库的注意事项
Considérations relatives à la référence de la bibliothèque externe par Visual Studio
B类网络子网划分
类模板重载输出运算符报错
[data analysis 02]
VScode运行C语言文件
数字化打开第二增长曲线,华为总结运营商云转型三大场景
IP address segment classification
Lihua Fast food ordering applet
Oracle sets the maximum number of connections
[rm_ee_note] 2 serial port & remote control
(pytorch进阶之路七)Attention Based Seq2Seq
Openshift 4 - install ODF and deploy red hat Quay (3 worker)
2022蓝桥杯省赛b组补题[九进制转十进制],[顺子日期],[刷题统计],[ 修剪灌木]
How PHP queries whether there are specified tables in MySQL database
websocket 传输文件
Digitalization opens the second growth curve. Huawei summarizes three scenarios of cloud transformation of operators
多线程一定能优化程序性能吗?
About object obj = new object()
[rm_ee_note] 1 gm6020 transceiver & simple PID debugging