# LeetCode 1260. 二维网格迁移

1.题目

2.题解

## 1.题目

输入：grid = [[1,2,3],[4,5,6],[7,8,9]], k = 1

输入：grid = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4

• 1 <= grid.length <= 50
• 1 <= grid[i].length <= 50
• -1000 <= grid[i][j] <= 1000
• 0 <= k <= 100

## 2.题解

public class Solution1260 {

@Test
public void test1260() {
int[][] grid = {{3, 8, 1, 9}, {19, 7, 2, 5}, {4, 6, 11, 10}, {12, 0, 21, 13}};
int k = 4;
System.out.println(shiftGrid(grid, k));
}

public List<List<Integer>> shiftGrid(int[][] grid, int k) {
int m = grid.length, n = grid[0].length;
while (k-- > 0) {
int[][] newGrid = new int[m][n];
for (int i = 0; i < m; i++) {
if (n - 1 >= 0) {
System.arraycopy(grid[i], 0, newGrid[i], 1, n - 1);
}
}
for (int i = 0; i < m - 1; i++) {
newGrid[i + 1][0] = grid[i][n - 1];
}
newGrid[0][0] = grid[m - 1][n - 1];
grid = newGrid;
}
List<List<Integer>> result = new ArrayList<>();
for (int[] g : grid) {
List<Integer> list = new ArrayList<>();
for (int t : g) {
}
}
return result;
}
}
• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

HarvestWu

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
11-17 75
01-31 113

04-05 58
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客