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

边栏推荐
- Efficiency increased by 98%! AI weapon behind operation and maintenance inspection of high altitude photovoltaic power station
- 动态规划-01背包滚动数组优化
- Sql文件导入数据库-保姆级教程
- Technical operation
- [acwing weekly rematch] 61st weekly 20220723
- Use SQLite provided by the system
- 2. Load test
- [Nuxt 3] (十)运行时配置
- The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode
- 技术操作
猜你喜欢

Quartus: install cyclone 10 LP device library for quartus version 17.1

Advanced function of postman
Simple message queue implementation nodejs + redis =mq

EF core :自引用的组织结构树
![[LeetCode周赛复盘] 第 303 场周赛20220724](/img/ba/0f16f1f42e4a2593ec0124f23b30d7.png)
[LeetCode周赛复盘] 第 303 场周赛20220724

C language: deep analysis function stack frame

每周小结(*66):下一个五年
![Why does [mindspore ascend] [custom operator] repeatedly assign values to one tensor affect another tensor?](/img/e3/135ac1e6eade70082c205d16ab8e34.jpg)
Why does [mindspore ascend] [custom operator] repeatedly assign values to one tensor affect another tensor?

The new version of SSM video tutorial in shangsilicon valley was released

Weekly summary (*66): next five years
随机推荐
Docker container Django + MySQL service
2022 the most NB JVM foundation to tuning notes, thoroughly understand Alibaba P6 small case
Optaplanner will abandon DRL (drools) scoring method!!!
Are you still using system. Currenttimemillis()? Take a look at stopwatch
Fast development board for Godson solid state drive startup (burning system to solid state) - partition
[mindspore ascend] [user defined operator] graph_ In mode, customize how to traverse tensor
你还在使用System.currentTimeMillis()?来看看StopWatch吧
Implement a avatar looping control
NVIDIA inspector detailed instructions
Efficiency increased by 98%! AI weapon behind operation and maintenance inspection of high altitude photovoltaic power station
Paper time review MB2: build a behavior model for autonomous databases
动态规划-01背包滚动数组优化
Opengauss kernel analysis: query rewriting
HTB-Aragog
SQL rewriting Series 6: predicate derivation
.net redis client newlife.redis.core library usage
Live broadcast preview | online seminar on open source security governance models and tools
数组中只出现一次的两个数字
The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode
Soft test --- fundamentals of programming language (Part 2)