Skip to content

Commit c9ccd04

Browse files
committed
二刷49
1 parent 55ebfa5 commit c9ccd04

File tree

4 files changed

+87
-18
lines changed

4 files changed

+87
-18
lines changed

docs/0049-group-anagrams.adoc

+50-18
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,67 @@
11
[#0049-group-anagrams]
2-
= 49. Group Anagrams
2+
= 49. 字母异位词分组
33

4-
{leetcode}/problems/group-anagrams/[LeetCode - Group Anagrams^]
4+
https://leetcode.cn/problems/group-anagrams/[LeetCode - 49. 字母异位词分组 ^]
55

6-
计数字符数量的解法有些精巧!这里想起来了波利亚在《怎样解题》中,着重强调的一点:一点要充分挖掘题目中的一直变量。题目中已经明确给出,所有的字符都是小写字符。但却没有意识到它的重要性!
6+
给你一个字符串数组,请你将 *字母异位词* 组合在一起。可以按任意顺序返回结果列表。
77

8-
Given an array of strings, group anagrams together.
8+
*字母异位词* 是由重新排列源单词的所有字母得到的一个新单词。
99

10-
*Example:*
10+
*示例 1:*
1111

12-
[subs="verbatim,quotes,macros"]
13-
----
14-
*Input:* `["eat", "tea", "tan", "ate", "nat", "bat"]`,
15-
*Output:*
16-
[
17-
["ate","eat","tea"],
18-
["nat","tan"],
19-
["bat"]
20-
]
21-
----
12+
....
13+
输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
14+
输出: [["bat"],["nat","tan"],["ate","eat","tea"]]
15+
....
16+
17+
*示例 2:*
18+
19+
....
20+
输入: strs = [""]
21+
输出: [[""]]
22+
....
23+
24+
*示例 3:*
25+
26+
....
27+
输入: strs = ["a"]
28+
输出: [["a"]]
29+
....
30+
31+
*提示:*
2232

23-
*Note:*
33+
* `1 \<= strs.length \<= 10^4^`
34+
* `+0 <= strs[i].length <= 100+`
35+
* `strs[i]` 仅包含小写字母
2436
2537
26-
* All inputs will be in lowercase.
27-
* The order of your output does not matter.
38+
== 思路分析
2839

40+
计数字符数量的解法有些精巧!这里想起来了波利亚在《怎样解题》中,着重强调的一点:一点要充分挖掘题目中的一直变量。题目中已经明确给出,所有的字符都是小写字符。但却没有意识到它的重要性!
2941

3042
[[src-0049]]
43+
[tabs]
44+
====
45+
一刷::
46+
+
47+
--
3148
[{java_src_attr}]
3249
----
3350
include::{sourcedir}/_0049_GroupAnagrams.java[tag=answer]
3451
----
52+
--
53+
54+
二刷::
55+
+
56+
--
57+
[{java_src_attr}]
58+
----
59+
include::{sourcedir}/_0049_GroupAnagrams_2.java[tag=answer]
60+
----
61+
--
62+
====
63+
64+
65+
== 参考资料
66+
3567

logbook/202503.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,11 @@ endif::[]
503503
|{doc_base_url}/0048-rotate-image.adoc[题解]
504504
|✅ 使用递归来推进循环。
505505

506+
|{counter:codes2503}
507+
|{leetcode_base_url}/group-anagrams/[49. Group Anagrams^]
508+
|{doc_base_url}/0049-group-anagrams.adoc[题解]
509+
|✅ 使用 `Map` 存字符和数量即可。
510+
506511
|===
507512

508513
截止目前,本轮练习一共完成 {codes2503} 道题。

src/main/java/com/diguage/algo/leetcode/_0049_GroupAnagrams.java

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public class _0049_GroupAnagrams {
3535
* Runtime: 16 ms, faster than 22.85% of Java online submissions for Group Anagrams.
3636
*
3737
* Memory Usage: 40.4 MB, less than 96.49% of Java online submissions for Group Anagrams.
38+
*
39+
* @author D瓜哥 · https://www.diguage.com
40+
* @since 2020-01-06 21:42
3841
*/
3942
public List<List<String>> groupAnagrams(String[] strs) {
4043
if (Objects.isNull(strs) || strs.length == 0) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public class _0049_GroupAnagrams_2 {
9+
// tag::answer[]
10+
/**
11+
* @author D瓜哥 · https://www.diguage.com
12+
* @since 2025-04-26 17:08:52
13+
*/
14+
public List<List<String>> groupAnagrams(String[] strs) {
15+
Map<Map<Character, Integer>, List<String>> map = new HashMap<>();
16+
for (String str : strs) {
17+
char[] chars = str.toCharArray();
18+
Map<Character, Integer> key = new HashMap<>();
19+
for (char c : chars) {
20+
key.put(c, key.getOrDefault(c, 0) + 1);
21+
}
22+
map.computeIfAbsent(key, k -> new ArrayList<>()).add(str);
23+
}
24+
List<List<String>> result = new ArrayList<>(map.size());
25+
result.addAll(map.values());
26+
return result;
27+
}
28+
// end::answer[]
29+
}

0 commit comments

Comments
 (0)