class Solution {
public List<List<Integer>> shiftGrid(int[][] grid, int k) {
List<List<Integer>> res = new ArrayList<>();
int m = grid.length, n = grid[0].length;
int[][] arr = new int[m][n];
for (int i = 0; i < m; i++){
for (int j = 0; j < n ;j++){
int temp = grid[i][j];
// 代表在当前行操作
if (k + j < n){
arr[i][j + k] = temp;
} else {
int x = j + k;
arr[(x / n + i ) % m][x % n] = temp;
}
}
}
for (int[] ints : arr) {
ArrayList<Integer> cur = new ArrayList<>();
for (int anInt : ints) {
cur.add(anInt);
}
res.add(cur);
}
return res;
}
}
力扣:1260. 二维网格迁移
最新推荐文章于 2025-12-01 21:54:04 发布
该博客详细介绍了如何在Java中实现矩阵的旋转操作。通过创建一个新的二维数组并利用循环逻辑,将原矩阵按指定步长k进行平移,实现了矩阵的顺时针或逆时针旋转。这种方法对于理解矩阵操作和算法思维具有一定的参考价值。

727

被折叠的 条评论
为什么被折叠?



