当前位置:网站首页>每日一题·1260.二维网络迁移·模拟
每日一题·1260.二维网络迁移·模拟
2022-07-20 14:43:00 【小迅想变强】
链接:https://leetcode.cn/problems/shift-2d-grid/solution/chun-c-by-xun-ge-v-7ky6/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
题目
示例
思路
题目已经告诉我们每次「迁移」操作将会引发下述活动:
- 位于 grid[i][j] 的元素将会移动到 grid[i][j + 1]。
- 位于 grid[i][n - 1] 的元素将会移动到 grid[i + 1][0]。
- 位于 grid[m - 1][n - 1] 的元素将会移动到 grid[0][0]。
直接按照题目给定要求进行模拟即可
小细节:利用取余对第三种特殊情况归并为第二种
代码
/**
* 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++)//初始化变量
{
res[i] = malloc(sizeof(int) * gridColSize[0]);
(*returnColumnSizes)[i] = gridColSize[0];
for(int j = 0; j < gridColSize[0]; j++)//将grid数组赋给res
{
res[i][j] = grid[i][j];
}
}
while(k)//直接模拟
{
for(int i = 0; i < gridSize; i++)//暴力循环遍历数组
{
for(int j = 0; j < gridColSize[0]; j++)
{
if(j == gridColSize[0]-1)//先对特殊情况进行处理
{
res[((i+1)%gridSize)][0] = grid[i][j];//利用取余对第三种特殊情况归并为第二种
}
else
{
res[i][j+1] = grid[i][j];
}
}
}
for(int i = 0; i < gridSize; i++)//改变原始数组
{
for(int j = 0; j < gridColSize[0]; j++)
{
grid[i][j] = res[i][j];
}
}
k--;
}
return res;
}
作者:xun-ge-v
链接:https://leetcode.cn/problems/shift-2d-grid/solution/chun-c-by-xun-ge-v-7ky6/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
时间空间复杂度
边栏推荐
- 【字体反爬】猫X眼YingShi,我们又来欺负你了,用到了 OCR 识别技术
- C语言力扣第九题之回文数。两指针数组遍历法
- 邀请函 | “人、财、税”数字化赋能,力促零售企业实现规模效益“倍增”
- 7.24聚会通知
- Hardware analysis and detailed explanation of RC series and parallel frequency selection network characteristics
- Using phpmailer to realize the function of sending email in thinkphp5
- Educational Codeforces Round 89 (Rated for Div. 2)ABC题解
- C#《学习代码片段》- 递归获取文件夹下所有文件
- Automated testing methods for SAP ui5 applications
- One of the tools of SAP ui5 system test: uiveri5
猜你喜欢
随机推荐
Deep understanding of high concurrency
A line break triggered thinking!
线性电路特性的研究与multisim仿真(附工程文件)
【组成原理 五 系统总线】
DOS汇编程序提高练习
中台的理解
【PyTorch】torch-geometric 安装
【HMS core】【Wallet Kit】【解决方案】华为钱包的客户端示例代码为何无法运行
如何用度量数据驱动代码评审的改善
Several ways to open SAP Hana Database Explorer in different locations
7.24聚会通知
一个换行符引发的思考!
SAP BTP 上 Roles,Roles collection 和 Scopes 的关联关系
【argoverse】argoverse-api 安装
One of the tools of SAP ui5 system test: uiveri5
Practice of data storage scheme in distributed system
Record locks
DOS汇编DEBUG基本命令及其功能详解
邀请函 | “人、财、税”数字化赋能,力促零售企业实现规模效益“倍增”
多线程进阶(下)