当前位置:网站首页>Force deduction solution summary 1260 two dimensional mesh migration
Force deduction solution summary 1260 two dimensional mesh migration
2022-07-22 19:32:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link : Power button
describe :
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
source : Power button (LeetCode)
link :https://leetcode.cn/problems/shift-2d-grid
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * Use an array with the same capacity grid2 Load new data , Then iterate through the array ,i,j The location data is moved to i1,j1 On . * j1=(j + k) % width; Remainder of certain sum width * i1 = i + (j + k) / width; It must be the divisor of the original height and width * If i1 Beyond the grid.length, be i1 = i1 % grid.length; that will do
Code :
public class Solution1260 {
public List<List<Integer>> shiftGrid(int[][] grid, int k) {
final int width = grid[0].length;
int[][] grid2 = new int[grid.length][width];
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < width; j++) {
int j1 = (j + k) % width;
int i1 = i + (j + k) / width;
i1 = i1 % grid.length;
grid2[i1][j1] = grid[i][j];
}
}
List<List<Integer>> result = new ArrayList<>();
for (int i = 0; i < grid2.length; i++) {
ArrayList<Integer> list = new ArrayList<>();
result.add(list);
for (int j = 0; j < width; j++) {
list.add(grid2[i][j]);
}
}
return result;
}
}
边栏推荐
猜你喜欢
随机推荐
Leetcode daily question 2022/2/14-2022/2/20
他的一只鸟,卖了6000万 ——明清大家八大山人藏品发售
LeetCode 每日一题 2022/1/31-2022/2/6
Global scope and function scope
Leetcode daily question 2022/1/17-2022/1/23
DOM add
Renjie, chief scientist of rongyun: experience produces talents, and career "experience > experience"
JS advanced - understanding of functions
“35岁,我退休了”:关于中年危机,这是最靠谱的回答
Go 中的基础库Time时间处理
Mail Informer
Swagger-UI介绍及常用注解说明
arguments
LeetCode 每日一题 2021/12/13-2021/12/19
LeetCode 每日一题 2022/2/28-2022/3/6
Force deduction solution summary 498 diagonal traversal
字符集和字符编码
Leetcode daily question 2022/1/10-2022/1/16
助力品牌洞察——消费者情绪行为分析
LeetCode 每日一题 2022/1/17-2022/1/23