当前位置:网站首页>1260. Two dimensional grid migration: simple construction simulation problem
1260. Two dimensional grid migration: simple construction simulation problem
2022-07-25 01:26: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 .
边栏推荐
- Harbor installation
- Take the first place in the International Olympic Games in mathematics, physics and chemistry, and win all the gold medals. Netizen: the Chinese team is too good
- Join MotoGP Monster Energy British Grand Prix!
- On let variable promotion
- [FAQ of waiting insurance] can the waiting insurance evaluation organization help with the waiting insurance rectification?
- Custom type
- The solution of displaying garbled code in SecureCRT
- Shell judges whether the file exists and whether the file size is 0
- Fabric. JS centered element
- ASP rs.open SQL, Conn, what does 3, 1 stand for in 3,1?
猜你喜欢

Custom type

DotNetCore. Cap notes

Open source demo | release of open source example of arcall applet

基于ABP实现DDD--领域逻辑和应用逻辑

Human cell prosci 4-1BB ligand recombinant protein scheme

Ireport export PDF font bold failure

Summary of the most complete MySQL data types in history (Part 2)

Breederdao's first proposal was released: the constitution of the Dao organization

BisinessCardGen

Tiktok iqiyi announced cooperation, long and short video handshake and reconciliation?
随机推荐
Why is the exclude or include attribute setting of the < keep alive > component invalid
Worthington carboxyl transfer carbonic anhydrase application and literature reference
Open source demo | release of open source example of arcall applet
From casting sword to defending sword: the way to build the efficiency platform of didi project
Unity image control and rawimage
ASP rs.open SQL, Conn, what does 3, 1 stand for in 3,1?
After burning up 130 billion yuan in ten years, vertical e-commerce will eventually enter the dust of history
10 commonly used data visualization tool software
Open source demo | release of open source example of arcall applet
Harbor installation
Create the first hbuilder project
How to obtain workers' coats and helmets in stray? How to obtain workers' helmets
[29. DFS depth is preferred]
7.18 - daily question - 408
MySQL series | log module
Resolution of multi thread conflict lock
2022/7/18-7/19
Summary of the most complete MySQL data types in history (Part 2)
How to better use the touchpad of notebook
The position of the nth occurrence of MySQL in the string