当前位置:网站首页>Daily question-leetcode1260-2d grid migration array mapping
Daily question-leetcode1260-2d grid migration array mapping
2022-07-21 15:48:00 【Li Fan, hurry up】
Note:
Expand into a one-dimensional array , In the question is to put the last K Move the position to the beginning
1、 Entire array flip
2、0 ~ k
Flip
3、k ~ n * m
Flip
You can achieve the effect , When flipping, you don't have to really expand it into a one-dimensional array , Establish a mapping relationship to swap
that will do
The code is as follows :
class Solution {
public:
int n, m;
void reverse(vector<vector<int>>& g, int start, int end){
for(int i = start, j = end - 1; i < j; i ++, j --)
swap(g[i / m][i % m], g[j / m][j % m]);
}
vector<vector<int>> shiftGrid(vector<vector<int>>& grid, int k) {
n = grid.size(), m = grid[0].size();
k %= n * m;
reverse(grid, 0, n * m);
reverse(grid, 0, k);
reverse(grid, k, n * m);
return grid;
}
};
边栏推荐
猜你喜欢
【愚公系列】2022年7月 Go教学课程 014-运算符之算术运算符
【DIP/简易数字图像处理系统实现】高分课程设计论文分享
conda虚拟环境默认路径如何修改
EasyGBS平台设置小技巧:如何隐藏平台web页面不被访问?
小程序毕设作品之微信疫苗预约小程序毕业设计(6)开题答辩PPT
Training of head and neck segmentation networks with shape prior on small datasets
8 月亚马逊云科技培训与认证课程,精彩不容错过!
每日一题-LeetCode1260-二维网格迁移-数组-映射
C语言三大结构
数据库约束&&MySQL进阶查询
随机推荐
Algorithm summary] 20 questions to complete bat interview - binary tree
Wechat vaccine reservation applet graduation design of applet completion work (3) background function
Deep understanding of perfect hash
小程序毕设作品之微信疫苗预约小程序毕业设计(2)小程序功能
i2c client驱动
数据库约束&&MySQL进阶查询
i2c adapter驱动
【DIP/简易数字图像处理系统实现】高分课程设计论文分享
学习白银k线图应该从哪几方面入手
获取当前 标注样式dimstyle
ESP8266-NodeMCU——使用U8g2库点亮OLED
通过TPT的FUSION平台实现联合测试
模仿vtk的Widget控件,制作画矩形的控件
小程序毕设作品之微信疫苗预约小程序毕业设计(7)中期检查报告
Classic competition questions web penetration test record
Training of head and neck segmentation networks with shape prior on small datasets
致测试/开发程序员:爱生活,不加班,苦逼的程序员......
[Star Project] small hat aircraft War (I)
投影&局部光照
c || 结构体 联合体 枚举类型