当前位置:网站首页>力扣解法汇总1260-二维网格迁移
力扣解法汇总1260-二维网格迁移
2022-07-22 08:55:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
给你一个 m 行 n 列的二维网格 grid 和一个整数 k。你需要将 grid 迁移 k 次。
每次「迁移」操作将会引发下述活动:
位于 grid[i][j] 的元素将会移动到 grid[i][j + 1]。
位于 grid[i][n - 1] 的元素将会移动到 grid[i + 1][0]。
位于 grid[m - 1][n - 1] 的元素将会移动到 grid[0][0]。
请你返回 k 次迁移操作后最终得到的 二维网格。
示例 1:
输入:grid = [[1,2,3],[4,5,6],[7,8,9]], k = 1
输出:[[9,1,2],[3,4,5],[6,7,8]]
示例 2:
输入:grid = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4
输出:[[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]
示例 3:
输入:grid = [[1,2,3],[4,5,6],[7,8,9]], k = 9
输出:[[1,2,3],[4,5,6],[7,8,9]]
提示:
m == grid.length
n == grid[i].length
1 <= m <= 50
1 <= n <= 50
-1000 <= grid[i][j] <= 1000
0 <= k <= 100
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/shift-2d-grid
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 用一个相同容量的数组grid2装载新的数据,然后遍历数组,i,j位置的数据被移动到i1,j1上。 * j1=(j + k) % width; 一定和宽度的余数 * i1 = i + (j + k) / width; 一定是原高度加宽度的除数 * 如果i1超出了grid.length,则i1 = i1 % grid.length;即可
代码:
public class Solution1260 {
public List<List<Integer>> shiftGrid(int[][] grid, int k) {
final int width = grid[0].length;
int[][] grid2 = new int[grid.length][width];
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < width; j++) {
int j1 = (j + k) % width;
int i1 = i + (j + k) / width;
i1 = i1 % grid.length;
grid2[i1][j1] = grid[i][j];
}
}
List<List<Integer>> result = new ArrayList<>();
for (int i = 0; i < grid2.length; i++) {
ArrayList<Integer> list = new ArrayList<>();
result.add(list);
for (int j = 0; j < width; j++) {
list.add(grid2[i][j]);
}
}
return result;
}
}
边栏推荐
- About the external entity key operation spinbox control in stemwin (fishing_2)
- QT notes - drag lines and movement of qtablewidget
- 力扣解法汇总731-我的日程安排表 II
- 一线互联网P7面试题总结---补充中
- 2022-07-21:给定一个字符串str,和一个正数k, 你可以随意的划分str成多个子串, 目的是找到在某一种划分方案中,有尽可能多的回文子串,长度>=k,
- Mutexes and semaphores
- visual studio 调试快捷键
- 请教下,oracle-cdc是不是不支持检查点,当实时采集过程中任务挂了到重启这段时间的数据变化是不
- flutter 线性布局让两个控件充满一行
- "Review of software engineering in Wuhan University of technology" Chapter 2 | software process model
猜你喜欢
Microsoft SQL Server数据库语言及功能使用(十二)
Applet sorted by an element of the structure (fishing_3)
按结构体某一元素排序的小程序(fishing_3)
【Rust】Rust 语言基础 | 学习语言都应该快速得出印象
Computer network learning notes 7-tcp programming process and interview questions
【Rust】为什么我建议你学一下 Rust | Rust 初探
Use of bullets in object pool mode in aircraft war
C#入门系列(二十七) -- LINQ简析
QT笔记——网络通信 之 QUdpSocket
"Review of software engineering in Wuhan University of technology" Chapter 6 | coding specification
随机推荐
pm的报警告:“npm WARN config global --global, --local are deprecated
Verilog for loop (1)
2021-10-18使用eop烧写裸板程序
Qt|编辑框的使用总结
女嘉宾报名
「武汉理工大学 软件工程复习」第七章 | 软件测试
Research on vulnerability identification technology for project version differences
QT exe只允许运行单个
Internet Download Manager2022试用版(简称 IDM)
Vim编辑器常用快捷方式
Research on network slicing security for 5g mmtc
43. String multiplication
关于STemwin中,外部实体按键操作Spinbox控件(fishing_2)
Installation and introduction of Lin UI component library of wechat applet - Advanced
QT笔记——QTableWidget表格生成树,QTreeWidget树节点生成表格内容
Computer network learning notes 7-tcp programming process and interview questions
"Review of software engineering in Wuhan University of technology" Chapter 7 | software testing
云原生(十) | Kubernetes篇之Kubernetes简介
What is the routing protocol
Hblock revitalizes the enterprise storage market