Skip to content

Commit 7319079

Browse files
committed
✨feat: Add 2022
1 parent 832accc commit 7319079

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

Index/模拟.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,6 @@
9292
| [1893. 检查是否区域内所有整数都被覆盖](https://leetcode-cn.com/problems/check-if-all-the-integers-in-a-range-are-covered/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/check-if-all-the-integers-in-a-range-are-covered/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-j83x/) | 简单 | 🤩🤩🤩🤩 |
9393
| [1894. 找到需要补充粉笔的学生编号](https://leetcode-cn.com/problems/find-the-student-that-will-replace-the-chalk/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-the-student-that-will-replace-the-chalk/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-kpqsk/) | 中等 | 🤩🤩🤩🤩 |
9494
| [1995. 统计特殊四元组](https://leetcode-cn.com/problems/count-special-quadruplets/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-special-quadruplets/solution/gong-shui-san-xie-yi-ti-si-jie-mei-ju-ha-gmhv/) | 简单 | 🤩🤩🤩🤩 |
95+
| [2022. 将一维数组转变成二维数组](https://leetcode-cn.com/problems/convert-1d-array-into-2d-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/convert-1d-array-into-2d-array/solution/gong-shui-san-xie-jiang-2021-de-1-gai-ch-qc1a/) | 简单 | 🤩🤩🤩🤩 |
9596
| [面试题 10.02. 变位词组](https://leetcode-cn.com/problems/group-anagrams-lcci/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/group-anagrams-lcci/solution/gong-shui-san-xie-tong-ji-bian-wei-ci-de-0iqe/) | 中等 | 🤩🤩🤩🤩 |
9697

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[2022. 将一维数组转变成二维数组](https://leetcode-cn.com/problems/convert-1d-array-into-2d-array/solution/gong-shui-san-xie-jiang-2021-de-1-gai-ch-qc1a/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
给你一个下标从 $0$ 开始的一维整数数组 `original` 和两个整数 `m` 和  `n` 。
10+
11+
你需要使用 `original` 中 所有 元素创建一个 `m` 行 `n` 列的二维数组。
12+
13+
`original` 中下标从 `0` 到 `n - 1` (都 包含 )的元素构成二维数组的第一行,下标从 `n` 到 `2 * n - 1` (都 包含 )的元素构成二维数组的第二行,依此类推。
14+
15+
请你根据上述过程返回一个 `m x n` 的二维数组。如果无法构成这样的二维数组,请你返回一个空的二维数组。
16+
17+
示例 1:
18+
![](https://assets.leetcode.com/uploads/2021/08/26/image-20210826114243-1.png)
19+
```
20+
输入:original = [1,2,3,4], m = 2, n = 2
21+
22+
输出:[[1,2],[3,4]]
23+
24+
解释:
25+
构造出的二维数组应该包含 2 行 2 列。
26+
original 中第一个 n=2 的部分为 [1,2] ,构成二维数组的第一行。
27+
original 中第二个 n=2 的部分为 [3,4] ,构成二维数组的第二行。
28+
```
29+
示例 2:
30+
```
31+
输入:original = [1,2,3], m = 1, n = 3
32+
33+
输出:[[1,2,3]]
34+
35+
解释:
36+
构造出的二维数组应该包含 1 行 3 列。
37+
将 original 中所有三个元素放入第一行中,构成要求的二维数组。
38+
```
39+
示例 3:
40+
```
41+
输入:original = [1,2], m = 1, n = 1
42+
43+
输出:[]
44+
45+
解释:
46+
original 中有 2 个元素。
47+
无法将 2 个元素放入到一个 1x1 的二维数组中,所以返回一个空的二维数组。
48+
```
49+
示例 4:
50+
```
51+
输入:original = [3], m = 1, n = 2
52+
53+
输出:[]
54+
55+
解释:
56+
original 中只有 1 个元素。
57+
无法将 1 个元素放满一个 1x2 的二维数组,所以返回一个空的二维数组。
58+
```
59+
60+
提示:
61+
* $1 <= original.length <= 5 * 10^4$
62+
* $1 <= original[i] <= 10^5$
63+
* $1 <= m, n <= 4 * 10^4$
64+
65+
---
66+
67+
### 模拟
68+
69+
**新年快乐,祝大家身体健康,各种上岸 🎉 🎉(带着 $2021$ 年的一切美好品质继续前行**
70+
71+
构造 $m * n$ 的新二维矩阵 `year2022`,并使用 $idx$ 对旧矩阵 `year2021` 进行遍历即可。
72+
73+
代码:
74+
```Java
75+
class Solution {
76+
public int[][] construct2DArray(int[] year2021, int m, int n) {
77+
if (year2021.length != m * n) return new int[0][0];
78+
int[][] year2022 = new int[m][n];
79+
for (int i = 0, idx = 0; i < m; i++) {
80+
for (int j = 0; j < n; j++) {
81+
year2022[i][j] = year2021[idx++];
82+
}
83+
}
84+
return year2022;
85+
}
86+
}
87+
```
88+
* 时间复杂度:$O(m * n)$
89+
* 空间复杂度:$O(m * n)$
90+
91+
---
92+
93+
### 最后
94+
95+
这是我们「刷穿 LeetCode」系列文章的第 `No.2021` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
96+
97+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
98+
99+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
100+
101+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
102+

0 commit comments

Comments
 (0)