当前位置:网站首页>LeetCode:1260. Two dimensional mesh migration [one dimensional expansion + splicing]
LeetCode:1260. Two dimensional mesh migration [one dimensional expansion + splicing]
2022-07-21 19:29:00 【Starry sky and bright moon】
Title Description
To give you one m That's ok n Two dimensional grid of columns grid And an integer k. You need to grid transfer k Time .
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].
Please return k The final result after the migration operation Two dimensional meshes .
Input and output
Input :grid = [[1,2,3],[4,5,6],[7,8,9]], k = 1
Output :[[9,1,2],[3,4,5],[6,7,8]]
Tips
- m == grid.length
- n == grid[i].length
- 1 <= m <= 50
- 1 <= n <= 50
- -1000 <= grid[i][j] <= 1000
- 0 <= k <= 100
Ideas
Expand two dimensions into one dimension , In fact, it is to move the one-dimensional array backward k, Then it is spliced into two dimensions .
AC Code
class Solution {
public:
vector<vector<int>> shiftGrid(vector<vector<int>>& grid, int k) {
int n = grid.size();
int m = grid[0].size();
vector<int> temp;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
temp.push_back(grid[i][j]);
}
}
int len = n * m;
int t = k;
int pos = 0;
t %= len;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (t > 0) {
grid[i][j] = temp[len - t];
t--;
} else {
grid[i][j] = temp[pos];
pos++;
}
}
}
return grid;
}
};
The time complexity and space complexity are both O(m*n)
边栏推荐
- Software testing interview question: what do you think are the advantages of testing?
- Distributed ID generator
- Software test interview question: bug management tool tracking process (using bugzilla as an example)
- 5. Customize global AuthenticationManager
- LBA转换成CHS公式
- Distributed load balancing
- 分布式.常用架构和服务拆分
- 分布式.容量评估
- Implementation method of SuperMap iclient for openlayers layer group control
- MYSQL08_ Overview of sub query, single row, multi row, related sub query
猜你喜欢
使用MogDB Operator在Kubernetes上部署MogDB集群(MogDB Stack)
载二氢丹参酮Ⅰ白蛋白纳米粒/去甲斑蝥素白蛋白纳米粒/伏立康唑白蛋白纳米粒的制备研究
MYSQL09_ Elaborate on database data types
Distributed Cap theory
ClickHouse深度揭秘
四种bean拷贝工具对比
Loop structure -- while loop and do while loop
MySQL advanced (b)
h5在微信内自定义分享遇到的坑
2022 software testing skills jmeter+ant+jenkins continuous integration and test report generation tutorial
随机推荐
Interview 02
DOM 事件流(事件捕获和事件冒泡)
分布式.CAP理论
Distributed Database schema
Hcip experiment the next day
Distributed Common architectures and service splitting
Distributed Cap theory
Recommended collection | practical operation, data console selection example
Kingbasees v8r6 is there any way to not read sys by default_ System view under catalog?
FPGA逻辑资源评估之BRAM(以Xilinx为例)
Teach you how to install MySQL database by hand
SAP smartforms print failure message type: ssfcomposer message number: 601 (currency and number field setting reference and format)
NFS share
分布式.ID生成器
建议收藏 | 可实操,数据中台选型示例
C# 数字信号处理工具包 DSP-Core 重采样(Resample)输出点数是多少
分布式.容量评估
JMeter learning notes 003-csv parameterization of JMeter
Software testing interview question: talk about your understanding of the two strategies of top-down integration and bottom-up integration in integration testing, and talk about their respective advan
Spark RDD, application case of spark SQL