当前位置:网站首页>Force deduction solution summary 1260 two dimensional mesh migration
Force deduction solution summary 1260 two dimensional mesh migration
2022-07-21 16:30: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;
}
}
边栏推荐
- [dip/ implementation of simple digital image processing system] high score course design paper sharing
- Why can't Alibaba cloud international account login?
- 【STC15控制WS2812 RGB彩灯级联】
- Dama Chapter 11 (data warehouse and business intelligence)
- -Solid modeling-
- What are the methods to realize timeout in go language
- [wechat applet] switch switch selector (81/100)
- Cad2014 x64 debugging ARX
- Shape and boundary-aware multi-branch model for semi-supervised medical image segmentation
- [JS] dynamic rendering page
猜你喜欢
Projection & local illumination
TransUNet: Transformers Make Strong Encoders for Medical Image Segmentation
Visual studio easy to use plug-in sharing
Signal integrity (SI) power integrity (PI) learning notes (XXIX) power distribution network (I)
In the future, a new way of tourism, VR traveling without leaving home, embracing poetry and distance
Le moteur 3D de dtos titos remplacera le moteur de jeu Giant Beast et réalisera la substitution de localisation
这款国产API神器工具也太强了吧...让我放弃了postman
[Star Project] small hat aircraft War (I)
[toggle 30 days of ML] Diabetes genetic risk detection challenge (1)
DTOS帝拓思的3D引擎將取代遊戲引擎巨獸,實現國產化替代
随机推荐
Use priority queue_ Queue implements unordered to base_ Value filtering of map on value value value
Excuse me, boss, how does Flink achieve real-time data synchronization between MySQL database and elastic search
Project summary: document management system
每日一题-LeetCode1260-二维网格迁移-数组-映射
Characteristics of let keyword
BluePrism工具菜单栏用户权限功能描述-RPA第二章
2022第五届中国(济南)国际青少年眼健康产业展览会9月举办
Imitate the widget control of VTK, and make the control that draws a rectangle
十大券商开户有没有风险的?安全靠谱吗?
Difference between service and systemctl / method of modifying path / a command to view IP address, gateway and DNS server
TransUNet: Transformers Make Strong Encoders for Medical Image Segmentation
Dam Chapter 13 (data quality management)
Realize joint testing through TPT fusion platform
修改标注样式文字填充颜色
What aspects should we start from to learn the silver K-line chart
C | file operation
Xqlla2.3.2 parsing query
I2C client Drive IIO Device Drive
Devaxpress treelist enables the selection of parent node column values and all its child nodes
What information can the real-time market of London Silver bring to people