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

边栏推荐
- [Nuxt 3] (十)运行时配置
- Implement a avatar looping control
- Install K6 test tool
- Kubernetes application design guide
- 云图
- Tencent low code platform is officially open source! You can drag and drop and generate mobile phone projects and PC projects! Get private benefits
- Nodejs package
- Qt项目-安防监控系统(各个界面功能实现)
- 云计算三类巨头:IaaS、PaaS、SaaS,分别是什么意思,应用场景是什么?
- [LeetCode周赛复盘] 第 83 场双周赛20220723
猜你喜欢

你还在使用System.currentTimeMillis()?来看看StopWatch吧

ROS manipulator movelt learning notes 3 | kinect360 camera (V1) related configuration

在混合云中管理数据库:八个关键注意事项

Redis memory analysis tool RMA usage

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

Soft test --- fundamentals of programming language (Part 2)

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

Processing PDF and JPG files in VB6

Multithreading & high concurrency (the latest in the whole network: interview questions + map + Notes) the interviewer is calm

ROS机械臂 Movelt 学习笔记3 | kinect360相机(v1)相关配置
随机推荐
Where are MySQL version numbers 6 and 7?
Paper time review MB2: build a behavior model for autonomous databases
Technical operation
LeetCode_6124_第一个出现两次的字母
What are the meanings and application scenarios of the three giants of cloud computing: IAAs, PAAS and SaaS?
Deep and direct visual slam
Kubernetes application design guide
Discussion on line segment tree
4. Immersion test
SQL result export function. If you click the work order but don't enter it, the interface is always blank and there is no response. What should you do?
Log4j configuration file
Are you still using system. Currenttimemillis()? Take a look at stopwatch
[mindspore] [mode] spontaneous_ The difference between mode and graph mode
Qt学习-利用数据库单例完成 登录匹配 + 注册 功能实现
Netease game Flink SQL platform practice
Heap and stack in embedded development
What is the function of transdata operator and whether it can optimize performance
Leetcode 0123. the best time to buy and sell stocks III: dynamic programming + simulation in constant space
Answers to some problems encountered in the process of Xperia XZ (f8332) brushing and root
Upload and download filask files