当前位置:网站首页>1260. Two dimensional grid migration: simple construction simulation problem
1260. Two dimensional grid migration: simple construction simulation problem
2022-07-25 01:20:00 【Gong Shui Sanye's Diary of writing questions】
Title Description
This is a LeetCode Upper 1260. Two dimensional grid migration , The difficulty is Simple .
Tag : 「 simulation 」、「 structure 」
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 togrid[i][j + 1]be located grid[i][n - 1]The element will be moved togrid[i + 1][0]be located grid[m - 1][n - 1]The element will be moved togrid[0][0]Please return kThe 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 :
simulation
For convenience , We make grid by g, Make n and m Respectively g The number of rows and columns .
Because the migration process has obvious regularity , So we can directly regard as The subscript of the final column of each column tcol = (i + k) % m( among i Subscript the original column ), meanwhile regard as The row subscript of the first row element of the current column in the new column trow = ((i + k) / m) % n, Then it is a simple traversal assignment operation .
Java Code :
class Solution {
public List<List<Integer>> shiftGrid(int[][] g, int k) {
int n = g.length, m = g[0].length;
int[][] mat = new int[n][m];
for (int i = 0; i < m; i++) {
int tcol = (i + k) % m, trow = ((i + k) / m) % n, idx = 0;
while (idx != n) {
mat[trow++][tcol] = g[idx++][i];
if (trow == n) trow = 0;
}
}
List<List<Integer>> ans = new ArrayList<>();
for (int i = 0; i < n; i++) {
List<Integer> alist = new ArrayList<>();
for (int j = 0; j < m; j++) alist.add(mat[i][j]);
ans.add(alist);
}
return ans;
}
}
TypeScript Code :
function shiftGrid(g: number[][], k: number): number[][] {
const n = g.length, m = g[0].length
const ans: number[][] = new Array<Array<number>>()
for (let i = 0; i < n; i++) ans[i] = new Array<number>(m).fill(0)
for (let i = 0; i < m; i++) {
let tcol = (i + k) % m, trow = Math.floor(((i + k) / m)) % n, idx = 0
while (idx != n) {
ans[trow++][tcol] = g[idx++][i]
if (trow == n) trow = 0
}
}
return ans
};
Time complexity : Spatial complexity :
Last
This is us. 「 Brush through LeetCode」 The first of the series No.1260 piece , The series begins with 2021/01/01, As of the start date LeetCode I have in common 1916 questions , Part of it is a locked question , We will finish all the questions without lock first .
In this series , In addition to explaining the idea of solving problems , And give the most concise code possible . If the general solution is involved, there will be corresponding code templates .
In order to facilitate the students to debug and submit code on the computer , I've built a warehouse :https://github.com/SharingSource/LogicStack-LeetCode .
In the warehouse address , You can see the links to the series 、 The corresponding code of the series 、LeetCode Links to the original problem and other preferred solutions .
This paper is written by mdnice Multi platform Publishing
边栏推荐
- Chapter III kernel development
- Yolov7:oserror: [winerror 1455] the page file is too small to complete the final solution of the operation
- Service address dynamic awareness of Nacos registry
- Prosci 14-3-3 (phosphate ser58) antibody instructions
- C recursively obtains all files under the folder and binds them to the treeview control
- Login and payment arrangement in uniapp
- asp rs.open sql,conn,3,1中3,1代表什么?
- Unity3d calls between different script functions or parameters
- 7.16 - daily question - 408
- MySQL series | log module
猜你喜欢

Windows security hardening -- close unnecessary ports

What is iftmcs indicating contract status message?
![[C + + primer notes] Chapter 8 IO Library](/img/84/53cca8bb00b392c74b5dfe039da711.png)
[C + + primer notes] Chapter 8 IO Library

Implementing DDD based on ABP -- domain logic and application logic

Divide 300000 bonus! Deeperec CTR model performance optimization Tianchi challenge is coming

What is the root password of MySQL initial installation

Kubernetes creates a user with dashboard read-only permission (with exec permission)

Moonpdflib Preview PDF usage record

7.18 - daily question - 408

Breederdao's first proposal was released: the constitution of the Dao organization
随机推荐
Screenshot of Baidu map
Solution to the shortest Hamilton path problem
Dynamic kubernetes cluster capacity expansion of airbnb
"Usaco2006nov" corn fields solution
This visual is not connected to the presentationsource.
[25. Hash table]
Why is the exclude or include attribute setting of the < keep alive > component invalid
Educational codeforces round 89 (rated for Div. 2) ABC problem solution
2012.4.13 360 written examination summary
Specificity and five applications of Worthington alcohol dehydrogenase
WPF implements RichTextBox keyword query highlighting
如何创建索引
Open source demo | release of open source example of arcall applet
Divide 300000 bonus! Deeperec CTR model performance optimization Tianchi challenge is coming
Codeworks round 651 (Div. 2) ABCD solution
On let variable promotion
PG Optimization -- execution plan
unresolved external symbol [email protected] resolvent
进程的几种状态
About the difference between for... In and for... Of and object. Keys()