Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f7c4659

Browse files
Merge pull request SharingSource#603 from SharingSource/ac_oier
✨feat: add 1260
2 parents 7cf5c3c + 4a7e6e1 commit f7c4659

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

‎Index/构造.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
| [406. 根据身高重建队列](https://leetcode.cn/problems/queue-reconstruction-by-height/) | [LeetCode 题解链接](https://leetcode.cn/problems/queue-reconstruction-by-height/solution/by-ac_oier-fda2/) | 中等 | 🤩🤩🤩🤩 |
55
| [942. 增减字符串匹配](https://leetcode.cn/problems/di-string-match/) | [LeetCode 题解链接](https://leetcode.cn/problems/di-string-match/solution/by-ac_oier-pvjk/) | 简单 | 🤩🤩🤩🤩🤩 |
66
| [961. 在长度 2N 的数组中找出重复 N 次的元素](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/solution/by-ac_oier-bslq/) | 简单 | 🤩🤩🤩🤩 |
7+
| [1260. 二维网格迁移](https://leetcode.cn/problems/shift-2d-grid/) | [LeetCode 题解链接](https://leetcode.cn/problems/shift-2d-grid/solution/by-ac_oier-1blt/) | 简单 | 🤩🤩🤩🤩 |
78
| [1537. 最大得分](https://leetcode.cn/problems/get-the-maximum-score/) | [LeetCode 题解链接](https://leetcode.cn/problems/get-the-maximum-score/solution/by-ac_oier-ht78/) | 困难 | 🤩🤩🤩🤩 |
89
| [1719. 重构一棵树的方案数](https://leetcode-cn.com/problems/number-of-ways-to-reconstruct-a-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-ways-to-reconstruct-a-tree/solution/gong-shui-san-xie-gou-zao-yan-zheng-he-f-q6fc/) | 困难 | 🤩🤩 |
910
| [2028. 找出缺失的观测数据](https://leetcode-cn.com/problems/find-missing-observations/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-missing-observations/solution/by-ac_oier-x22k/) | 中等 | 🤩🤩🤩🤩🤩 |

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
| [1185. 一周中的第几天](https://leetcode-cn.com/problems/day-of-the-week/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/day-of-the-week/solution/gong-shui-san-xie-jian-dan-ri-qi-tong-ji-czt6/) | 简单 | 🤩🤩🤩🤩 |
130130
| [1189. "气球" 的最大数量](https://leetcode-cn.com/problems/maximum-number-of-balloons/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-number-of-balloons/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-9px4/) | 简单 | 🤩🤩🤩🤩 |
131131
| [1252. 奇数值单元格的数目](https://leetcode.cn/problems/cells-with-odd-values-in-a-matrix/) | [LeetCode 题解链接](https://leetcode.cn/problems/cells-with-odd-values-in-a-matrix/solution/by-ac_oier-p0za/) | 简单 | 🤩🤩🤩 |
132+
| [1260. 二维网格迁移](https://leetcode.cn/problems/shift-2d-grid/) | [LeetCode 题解链接](https://leetcode.cn/problems/shift-2d-grid/solution/by-ac_oier-1blt/) | 简单 | 🤩🤩🤩🤩 |
132133
| [1332. 删除回文子序列](https://leetcode-cn.com/problems/remove-palindromic-subsequences/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/remove-palindromic-subsequences/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-0zwn/) | 中等 | 🤩🤩🤩🤩 |
133134
| [1342. 将数字变成 0 的操作次数](https://leetcode-cn.com/problems/number-of-steps-to-reduce-a-number-to-zero/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-steps-to-reduce-a-number-to-zero/solution/gong-shui-san-xie-note-bie-pian-yi-ti-sh-85fb/) | 简单 | 🤩🤩🤩🤩 |
134135
| [1380. 矩阵中的幸运数](https://leetcode-cn.com/problems/lucky-numbers-in-a-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/lucky-numbers-in-a-matrix/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-9xwg/) | 简单 | 🤩🤩🤩 |
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1260. 二维网格迁移](https://leetcode.cn/problems/shift-2d-grid/solution/by-ac_oier-1blt/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」、「构造」
6+
7+
8+
9+
给你一个 `m``n` 列的二维网格 `grid` 和一个整数 `k`。你需要将 `grid` 迁移 `k` 次。
10+
11+
每次「迁移」操作将会引发下述活动:
12+
13+
* 位于 `grid[i][j]` 的元素将会移动到 `grid[i][j + 1]`
14+
* 位于 `grid[i][n - 1]` 的元素将会移动到 `grid[i + 1][0]`
15+
* 位于 `grid[m - 1][n - 1]` 的元素将会移动到 `grid[0][0]`
16+
* 请你返回 `k` 次迁移操作后最终得到的 二维网格
17+
18+
示例 1:
19+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/11/16/e1-1.png)
20+
```
21+
输入:grid = [[1,2,3],[4,5,6],[7,8,9]], k = 1
22+
23+
输出:[[9,1,2],[3,4,5],[6,7,8]]
24+
```
25+
示例 2:
26+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/11/16/e2-1.png)
27+
```
28+
输入:grid = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4
29+
30+
输出:[[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]
31+
```
32+
示例 3:
33+
```
34+
输入:grid = [[1,2,3],[4,5,6],[7,8,9]], k = 9
35+
36+
输出:[[1,2,3],[4,5,6],[7,8,9]]
37+
```
38+
39+
提示:
40+
* $m == grid.length$
41+
* $n == grid[i].length$
42+
* 1ドル <= m <= 50$
43+
* 1ドル <= n <= 50$
44+
* $-1000 <= grid[i][j] <= 1000$
45+
* 0ドル <= k <= 100$
46+
47+
---
48+
49+
### 模拟
50+
51+
为了方便,我们令 `grid``g`,令 `n``m` 分别为 `g` 的行数和列数。
52+
53+
由于迁移过程存在明显规律性,因此我们可以直接 $O(1)$ 算得**每一列最终所在的列下标** `tcol = (i + k) % m`(其中 `i` 为原本的列下标),同时 $O(1)$ 算得**当前列的首行元素在新列中的行下标** `trow = ((i + k) / m) % n`,之后就是简单的遍历赋值操作。
54+
55+
Java 代码:
56+
```Java
57+
class Solution {
58+
public List<List<Integer>> shiftGrid(int[][] g, int k) {
59+
int n = g.length, m = g[0].length;
60+
int[][] mat = new int[n][m];
61+
for (int i = 0; i < m; i++) {
62+
int tcol = (i + k) % m, trow = ((i + k) / m) % n, idx = 0;
63+
while (idx != n) {
64+
mat[trow++][tcol] = g[idx++][i];
65+
if (trow == n) trow = 0;
66+
}
67+
}
68+
List<List<Integer>> ans = new ArrayList<>();
69+
for (int i = 0; i < n; i++) {
70+
List<Integer> alist = new ArrayList<>();
71+
for (int j = 0; j < m; j++) alist.add(mat[i][j]);
72+
ans.add(alist);
73+
}
74+
return ans;
75+
}
76+
}
77+
```
78+
TypeScript 代码:
79+
```TypeScript
80+
function shiftGrid(g: number[][], k: number): number[][] {
81+
const n = g.length, m = g[0].length
82+
const ans: number[][] = new Array<Array<number>>()
83+
for (let i = 0; i < n; i++) ans[i] = new Array<number>(m).fill(0)
84+
for (let i = 0; i < m; i++) {
85+
let tcol = (i + k) % m, trow = Math.floor(((i + k) / m)) % n, idx = 0
86+
while (idx != n) {
87+
ans[trow++][tcol] = g[idx++][i]
88+
if (trow == n) trow = 0
89+
}
90+
}
91+
return ans
92+
};
93+
```
94+
* 时间复杂度:$O(n \times m)$
95+
* 空间复杂度:$O(n \times m)$
96+
97+
---
98+
99+
### 最后
100+
101+
这是我们「刷穿 LeetCode」系列文章的第 `No.1260` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
102+
103+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
104+
105+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
106+
107+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
108+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /