当前位置:网站首页>Daily question 1 · 1260. Two dimensional network migration · simulation
Daily question 1 · 1260. Two dimensional network migration · simulation
2022-07-25 00:14:00 【Xiao Xun wants to be strong】
link :https://leetcode.cn/problems/shift-2d-grid/solution/chun-c-by-xun-ge-v-7ky6/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
subject

Example

Ideas
The topic has been told to us 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].
Directly simulate according to the given requirements of the topic
Little details : Use the remainder to merge the third special case into the second
Code
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int** shiftGrid(int** grid, int gridSize, int* gridColSize, int k, int* returnSize, int** returnColumnSizes){
int ** res = malloc(sizeof(int *) * gridSize);
*returnColumnSizes = malloc(sizeof(int) * gridSize);
*returnSize = gridSize;
for(int i = 0; i < gridSize; i++)// Initialize variable
{
res[i] = malloc(sizeof(int) * gridColSize[0]);
(*returnColumnSizes)[i] = gridColSize[0];
for(int j = 0; j < gridColSize[0]; j++)// take grid Array to res
{
res[i][j] = grid[i][j];
}
}
while(k)// Direct simulation
{
for(int i = 0; i < gridSize; i++)// Brute force loop traversal array
{
for(int j = 0; j < gridColSize[0]; j++)
{
if(j == gridColSize[0]-1)// First deal with special situations
{
res[((i+1)%gridSize)][0] = grid[i][j];// Use the remainder to merge the third special case into the second
}
else
{
res[i][j+1] = grid[i][j];
}
}
}
for(int i = 0; i < gridSize; i++)// Change the original array
{
for(int j = 0; j < gridColSize[0]; j++)
{
grid[i][j] = res[i][j];
}
}
k--;
}
return res;
}
author :xun-ge-v
link :https://leetcode.cn/problems/shift-2d-grid/solution/chun-c-by-xun-ge-v-7ky6/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .Time and space complexity

边栏推荐
- 做一个文艺的测试/开发程序员,慢慢改变自己......
- Restructuredtext grammar summary for beginners
- Processing of ffmpeg wasapi can't activate audio endpoint error
- Branch and loop statements in C language learning
- From the big guy baptism! 2022 headline first hand play MySQL advanced notes, and it is expected to penetrate P7
- 2022 Henan Mengxin League game (2): Henan University of technology d-pair
- SQL file import database - Nanny level tutorial
- Install software on kubernetes cluster using helm 3 package manager
- Paper time review MB2: build a behavior model for autonomous databases
- Multithreading & high concurrency (the latest in the whole network: interview questions + map + Notes) the interviewer is calm
猜你喜欢

Paper time review MB2: build a behavior model for autonomous databases

From the big guy baptism! 2022 headline first hand play MySQL advanced notes, and it is expected to penetrate P7

指针与数组

Redis memory analysis tool RMA usage

Be an artistic test / development programmer and slowly change yourself

Wechat applet development learning 5 (custom components)

Two numbers that appear only once in the array

NXP i.mx6q development board software and hardware are all open source, and the schematic diagram of the core board is provided

Development direction and problems of optaplanner

Quartus:17.1版本的Quartus安装Cyclone 10 LP器件库
随机推荐
SQLite database operation
Why do I have to clean up data?
HTB-Aragog
Can Baidu network disk yundetectservice.exe be disabled and closed
2022 the most NB JVM foundation to tuning notes, thoroughly understand Alibaba P6 small case
QT project - security monitoring system (function realization of each interface)
C语言学习之分支与循环语句
ROS机械臂 Movelt 学习笔记3 | kinect360相机(v1)相关配置
C language: deep analysis function stack frame
[hero planet July training leetcode problem solving daily] 24th line segment tree
Promtool Check
Internal network mapping port to external network
[acwing weekly rematch] 61st weekly 20220723
[untitled]
Where are MySQL version numbers 6 and 7?
LeetCode_392_判断子序列
How painful is it to write unit tests? Can you do it
Promtool Check
RS note: industry recommendation system YouTube DNN model (recall layer + sorting layer) [2016 youtube]
UART