当前位置:网站首页>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

边栏推荐
- Video chat source code - one-to-one live broadcast system source code
- Dynamic programming-01 knapsack rolling array optimization
- Managing databases in a hybrid cloud: eight key considerations
- [help] mindspire training based on ascend910 cannot reproduce the model effect on GPU
- NFT chain game system development metauniverse gamefi construction
- SQL file import database - Nanny level tutorial
- Promtool Check
- Redis6.2 SYSTEMd startup prompt redis service: Failed with result ‘protocol‘.
- 采坑记录:TypeError: 'module' object is not callable
- 多线程&高并发(全网最新:面试题 + 导图 + 笔记)面试手稳心不慌
猜你喜欢

2022 the most NB JVM foundation to tuning notes, thoroughly understand Alibaba P6 small case

Be an artistic test / development programmer and slowly change yourself

云计算三类巨头:IaaS、PaaS、SaaS,分别是什么意思,应用场景是什么?

Qt学习-利用数据库单例完成 登录匹配 + 注册 功能实现

指针与数组
![[acwing weekly rematch] 61st weekly 20220723](/img/8b/df2c8d516db1e7e5f2d50bcf62b2b1.png)
[acwing weekly rematch] 61st weekly 20220723

Heap and stack in embedded development

Kubernetes application design guide

@Mapkey usage instructions
![[leetcode weekly replay] game 83 biweekly 20220723](/img/db/c264c94ca3307d4363d3cf7f5d770b.png)
[leetcode weekly replay] game 83 biweekly 20220723
随机推荐
你还在使用System.currentTimeMillis()?来看看StopWatch吧
ROS机械臂 Movelt 学习笔记3 | kinect360相机(v1)相关配置
Flash send email
Leetcode 1260. two dimensional grid migration: two solutions (k simulations / one step)
EF core :自引用的组织结构树
UART
Processing PDF and JPG files in VB6
Click the "native practice" search box to expand the special effect so that you can realize it. How will you realize it?
Docker container Django + MySQL service
Efficiency increased by 98%! AI weapon behind operation and maintenance inspection of high altitude photovoltaic power station
Video chat source code - one-to-one live broadcast system source code
Heap and stack in embedded development
[nuxt 3] (x) runtime configuration
NFT chain game system development metauniverse gamefi construction
线段树杂谈
1. Smoke test
From the big guy baptism! 2022 headline first hand play MySQL advanced notes, and it is expected to penetrate P7
codeforces round #805 ABCDEFG
The new version of SSM video tutorial in shangsilicon valley was released
Leetcode 0125. validate palindrome string