Skip to content

Commit dd64713

Browse files
committed
update: 添加问题“2610.转换二维数组”的代码和题解(#825)
真是飞来横祸,莫名就躺枪了 Signed-off-by: LetMeFly666 <[email protected]>
1 parent feccb9a commit dd64713

11 files changed

+287
-4
lines changed

.commitmsg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
feat: under merge标签(#821)
1+
2+
真是飞来横祸,莫名就躺枪了
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-19 19:54:08
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-19 19:55:17
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
vector<vector<int>> findMatrix(vector<int>& nums) {
14+
vector<vector<int>> ans;
15+
unordered_map<int, int> cnt;
16+
for (int t : nums) {
17+
cnt[t]++;
18+
if (cnt[t] > ans.size()) {
19+
ans.push_back({});
20+
}
21+
ans[cnt[t] - 1].push_back(t);
22+
}
23+
return ans;
24+
}
25+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-19 20:03:37
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-19 20:05:02
6+
*/
7+
package main
8+
9+
func findMatrix(nums []int) (ans [][]int) {
10+
ma := map[int]int{}
11+
for _, t := range nums {
12+
ma[t]++
13+
if ma[t] > len(ans) {
14+
ans = append(ans, make([]int, 0))
15+
}
16+
ans[ma[t] - 1] = append(ans[ma[t]-1], t)
17+
}
18+
return
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-19 19:58:16
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-19 20:03:10
6+
*/
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
13+
class Solution {
14+
public List<List<Integer>> findMatrix(int[] nums) {
15+
List<List<Integer>> ans = new ArrayList<>();
16+
Map<Integer, Integer> ma = new HashMap<>();
17+
for (int t : nums) {
18+
ma.merge(t, 1, Integer::sum);
19+
if (ma.get(t) > ans.size()) {
20+
ans.add(new ArrayList<>());
21+
}
22+
ans.get(ma.get(t) - 1).add(t);
23+
}
24+
return ans;
25+
}
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-03-19 19:56:03
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-03-19 19:57:12
6+
'''
7+
from typing import List
8+
from collections import defaultdict
9+
10+
11+
class Solution:
12+
def findMatrix(self, nums: List[int]) -> List[List[int]]:
13+
ans: List[List[int]] = []
14+
ma = defaultdict(int)
15+
for t in nums:
16+
ma[t] += 1
17+
if ma[t] > len(ans):
18+
ans.append([])
19+
ans[ma[t] - 1].append(t)
20+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,7 @@
780780
|2597.美丽子集的数目|中等|<a href="https://leetcode.cn/problems/the-number-of-beautiful-subsets/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/07/LeetCode%202597.%E7%BE%8E%E4%B8%BD%E5%AD%90%E9%9B%86%E7%9A%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146108576" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/the-number-of-beautiful-subsets/solutions/3601438/letmefly-2597mei-li-zi-ji-de-shu-mu-er-j-bqpi/" target="_blank">LeetCode题解</a>|
781781
|2600.K件物品的最大和|简单|<a href="https://leetcode.cn/problems/k-items-with-the-maximum-sum/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/07/05/LeetCode%202600.K%E4%BB%B6%E7%89%A9%E5%93%81%E7%9A%84%E6%9C%80%E5%A4%A7%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131547457" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/k-items-with-the-maximum-sum/solutions/2331120/letmefly-2600k-jian-wu-pin-de-zui-da-he-08kcp/" target="_blank">LeetCode题解</a>|
782782
|2609.最长平衡子字符串|简单|<a href="https://leetcode.cn/problems/find-the-longest-balanced-substring-of-a-binary-string/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/11/08/LeetCode%202609.%E6%9C%80%E9%95%BF%E5%B9%B3%E8%A1%A1%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134296484" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-longest-balanced-substring-of-a-binary-string/solutions/2518443/letmefly-2609zui-chang-ping-heng-zi-zi-f-bkn5/" target="_blank">LeetCode题解</a>|
783+
|2610.转换二维数组|中等|<a href="https://leetcode.cn/problems/convert-an-array-into-a-2d-array-with-conditions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/19/LeetCode%202610.%E8%BD%AC%E6%8D%A2%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/https://leetcode.cn/problems/convert-an-array-into-a-2d-array-with-conditions/solutions/3619707/letmefly-2610zhuan-huan-er-wei-shu-zu-ha-hjpa/" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/convert-an-array-into-a-2d-array-with-conditions/solutions/3619707/letmefly-2610zhuan-huan-er-wei-shu-zu-ha-hjpa/" target="_blank">LeetCode题解</a>|
783784
|2611.老鼠和奶酪|中等|<a href="https://leetcode.cn/problems/mice-and-cheese/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/06/07/LeetCode%202611.%E8%80%81%E9%BC%A0%E5%92%8C%E5%A5%B6%E9%85%AA/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131085720" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/mice-and-cheese/solutions/2300325/letmefly-2611lao-shu-he-nai-luo-pai-xu-t-axv5/" target="_blank">LeetCode题解</a>|
784785
|2614.对角线上的质数|简单|<a href="https://leetcode.cn/problems/prime-in-diagonal/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/18/LeetCode%202614.%E5%AF%B9%E8%A7%92%E7%BA%BF%E4%B8%8A%E7%9A%84%E8%B4%A8%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146356303" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/prime-in-diagonal/solutions/3618431/letmefly-2614dui-jiao-xian-shang-de-zhi-r77y1/" target="_blank">LeetCode题解</a>|
785786
|2639.查询网格图中每一列的宽度|简单|<a href="https://leetcode.cn/problems/find-the-width-of-columns-of-a-grid/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/04/27/LeetCode%202639.%E6%9F%A5%E8%AF%A2%E7%BD%91%E6%A0%BC%E5%9B%BE%E4%B8%AD%E6%AF%8F%E4%B8%80%E5%88%97%E7%9A%84%E5%AE%BD%E5%BA%A6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/138244991" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-width-of-columns-of-a-grid/solutions/2757678/letmefly-2639cha-xun-wang-ge-tu-zhong-me-w02k/" target="_blank">LeetCode题解</a>|
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
---
2+
title: 2610.转换二维数组:哈希表(一次遍历)
3+
date: 2025-03-19 20:06:36
4+
tags: [题解, LeetCode, 中等, 数组, 哈希表]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】2610.转换二维数组:哈希表(一次遍历)
9+
10+
力扣题目链接:[https://leetcode.cn/problems/convert-an-array-into-a-2d-array-with-conditions/](https://leetcode.cn/problems/convert-an-array-into-a-2d-array-with-conditions/)
11+
12+
<p>给你一个整数数组 <code>nums</code> 。请你创建一个满足以下条件的二维数组:</p>
13+
14+
<ul>
15+
<li>二维数组应该 <strong>只</strong> 包含数组 <code>nums</code> 中的元素。</li>
16+
<li>二维数组中的每一行都包含 <strong>不同</strong> 的整数。</li>
17+
<li>二维数组的行数应尽可能 <strong>少</strong> 。</li>
18+
</ul>
19+
20+
<p>返回结果数组。如果存在多种答案,则返回其中任何一种。</p>
21+
22+
<p>请注意,二维数组的每一行上可以存在不同数量的元素。</p>
23+
24+
<p>&nbsp;</p>
25+
26+
<p><strong>示例 1:</strong></p>
27+
28+
<pre><strong>输入:</strong>nums = [1,3,4,1,2,3,1]
29+
<strong>输出:</strong>[[1,3,4,2],[1,3],[1]]
30+
<strong>解释:</strong>根据题目要求可以创建包含以下几行元素的二维数组:
31+
- 1,3,4,2
32+
- 1,3
33+
- 1
34+
nums 中的所有元素都有用到,并且每一行都由不同的整数组成,所以这是一个符合题目要求的答案。
35+
可以证明无法创建少于三行且符合题目要求的二维数组。</pre>
36+
37+
<p><strong>示例 2:</strong></p>
38+
39+
<pre><strong>输入:</strong>nums = [1,2,3,4]
40+
<strong>输出:</strong>[[4,3,2,1]]
41+
<strong>解释:</strong>nums 中的所有元素都不同,所以我们可以将其全部保存在二维数组中的第一行。
42+
</pre>
43+
44+
<p>&nbsp;</p>
45+
46+
<p><strong>提示:</strong></p>
47+
48+
<ul>
49+
<li><code>1 &lt;= nums.length &lt;= 200</code></li>
50+
<li><code>1 &lt;= nums[i] &lt;= nums.length</code></li>
51+
</ul>
52+
53+
54+
55+
## 解题方法:哈希表
56+
57+
使用一个哈希表统计每个数出现的次数。
58+
59+
遍历nums数组,统计当前元素的出现次数。若当前元素的出现次数大于答案数组的长度,就向答案数组中添加一个空数组。最后,将当前元素t加入$ans[出现次数-1]$中。
60+
61+
+ 时间复杂度$O(len(nums))$
62+
+ 空间复杂度$O(len(nums))$
63+
64+
### AC代码
65+
66+
#### C++
67+
68+
```cpp
69+
/*
70+
* @Author: LetMeFly
71+
* @Date: 2025-03-19 19:54:08
72+
* @LastEditors: LetMeFly.xyz
73+
* @LastEditTime: 2025-03-19 19:55:17
74+
*/
75+
class Solution {
76+
public:
77+
vector<vector<int>> findMatrix(vector<int>& nums) {
78+
vector<vector<int>> ans;
79+
unordered_map<int, int> cnt;
80+
for (int t : nums) {
81+
cnt[t]++;
82+
if (cnt[t] > ans.size()) {
83+
ans.push_back({});
84+
}
85+
ans[cnt[t] - 1].push_back(t);
86+
}
87+
return ans;
88+
}
89+
};
90+
```
91+
92+
#### Python
93+
94+
```python
95+
'''
96+
Author: LetMeFly
97+
Date: 2025-03-19 19:56:03
98+
LastEditors: LetMeFly.xyz
99+
LastEditTime: 2025-03-19 19:57:12
100+
'''
101+
from typing import List
102+
from collections import defaultdict
103+
104+
105+
class Solution:
106+
def findMatrix(self, nums: List[int]) -> List[List[int]]:
107+
ans: List[List[int]] = []
108+
ma = defaultdict(int)
109+
for t in nums:
110+
ma[t] += 1
111+
if ma[t] > len(ans):
112+
ans.append([])
113+
ans[ma[t] - 1].append(t)
114+
return ans
115+
116+
```
117+
118+
#### Java
119+
120+
```java
121+
/*
122+
* @Author: LetMeFly
123+
* @Date: 2025-03-19 19:58:16
124+
* @LastEditors: LetMeFly.xyz
125+
* @LastEditTime: 2025-03-19 20:03:10
126+
*/
127+
import java.util.HashMap;
128+
import java.util.Map;
129+
import java.util.ArrayList;
130+
import java.util.List;
131+
132+
133+
class Solution {
134+
public List<List<Integer>> findMatrix(int[] nums) {
135+
List<List<Integer>> ans = new ArrayList<>();
136+
Map<Integer, Integer> ma = new HashMap<>();
137+
for (int t : nums) {
138+
ma.merge(t, 1, Integer::sum);
139+
if (ma.get(t) > ans.size()) {
140+
ans.add(new ArrayList<>());
141+
}
142+
ans.get(ma.get(t) - 1).add(t);
143+
}
144+
return ans;
145+
}
146+
}
147+
```
148+
149+
#### Go
150+
151+
```go
152+
/*
153+
* @Author: LetMeFly
154+
* @Date: 2025-03-19 20:03:37
155+
* @LastEditors: LetMeFly.xyz
156+
* @LastEditTime: 2025-03-19 20:05:02
157+
*/
158+
package main
159+
160+
func findMatrix(nums []int) (ans [][]int) {
161+
ma := map[int]int{}
162+
for _, t := range nums {
163+
ma[t]++
164+
if ma[t] > len(ans) {
165+
ans = append(ans, make([]int, 0))
166+
}
167+
ans[ma[t] - 1] = append(ans[ma[t]-1], t)
168+
}
169+
return
170+
}
171+
```
172+
173+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/146379435/)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/03/19/LeetCode%202610.%E8%BD%AC%E6%8D%A2%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84/)哦~
174+
>
175+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,8 @@ categories: [自用]
10521052
|||
10531053
|prudence|n. 谨慎,节俭,精明|
10541054
|dramatist|n. 剧作家,编剧|
1055+
|||
1056+
|dike|n. 提防,渠,岩脉,女同性恋者<br/>v. 筑堤防护,开沟排水|
10551057

10561058
<p class="wordCounts">单词收录总数</p>
10571059

Solutions/Other-Japanese-LearningNotes.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ xx型
421421
|車(くるま)|汽车|
422422
|自転車(じてんしゃ)|自行车|
423423
|自動車(じどうしゃ)||
424-
|バス|公交车|
424+
|バス|公交车/巴士|
425+
|シャトルバス|穿梭巴士|
425426
|地下鉄(ちかてつ)|地铁|
426427
|タクシー|出租车|
427428
|空港(くうこう)|机场|
@@ -463,6 +464,8 @@ xx型
463464
|花瓶(かびん)|花瓶|
464465
|携帯(けいたい)|手机|
465466
|薬(くすり)||
467+
|ゴミ|垃圾|
468+
|広告(こうこく)|广告|
466469
|||
467470
|シャツ|衬衫|
468471
|セーター|毛衣|
@@ -1280,7 +1283,7 @@ xx型
12801283
|アンドリューさんの奥さんはいましたか。<br/>安德鲁的夫人在吗?|
12811284
|建物は新しかったです。<br/>建筑是新的。|
12821285
|ダニエルさんの奥さんは素敵な人でした。<br/>丹尼尔的夫人是个很棒的人。|
1283-
|<br/>|
1286+
|広告でホテルを見つけました。<br/>我在广告上发现这家酒店。|
12841287
|<br/>|
12851288
|<br/>|
12861289
|<br/>|

Solutions/Other-Notes-Mianjing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ MySQL5.5后默认使用InnoDB作为存储引擎。**建表**时聚簇索引:
270270
对于联合索引`(a, b)`,执行语句`select * from table where a > 1 and b = 2`的时候,只有`a`字段能用到索引。在B+树中搜索到`a > 1`的范围后,还需要看`b = 2`是否满足。
271271

272272
+ 在MySQL5.6之前,只能从主键值一个个回表,到主键索引上找出数据行并判断是否满足`b = 2`
273-
+ 在MySQL5.6引入了**索引下推优化**(index condition pushdown, ICP),可以在联合索引遍历国策红肿对联合索引中包含的字段做判断,直接过滤掉不满足条件的记录,减少回表次数。
273+
+ 在MySQL5.6引入了**索引下推优化**(index condition pushdown, ICP),可以在联合索引遍历过程中对联合索引中包含的字段做判断,直接过滤掉不满足条件的记录,减少回表次数。
274274

275275
下推其实就是将部分上层(服务层)负责的事情交给了下层(引擎层)去处理。
276276

0 commit comments

Comments
 (0)