Skip to content

Commit 59d9f7b

Browse files
committed
feat: add solutions to lcci problem: No.10.02
1 parent 838e4e3 commit 59d9f7b

File tree

31 files changed

+245
-103
lines changed

31 files changed

+245
-103
lines changed

lcci/10.02.Group Anagrams/README.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,92 @@
3030

3131
<!-- 这里可写通用的实现逻辑 -->
3232

33+
遍历字符串,将每个字符串按照字符字典序排序后得到一个新的字符串,将相同的新字符串放在哈希表的同一个 key 对应 value 列表中。
34+
35+
| key | value |
36+
| ------- | ----------------------- |
37+
| `"aet"` | `["eat", "tea", "ate"]` |
38+
| `"ant"` | `["tan", "nat"] ` |
39+
| `"abt"` | `["bat"] ` |
40+
41+
最后返回哈希表的 value 列表即可。
42+
3343
<!-- tabs:start -->
3444

3545
### **Python3**
3646

3747
<!-- 这里可写当前语言的特殊实现逻辑 -->
3848

3949
```python
40-
50+
class Solution:
51+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
52+
chars = collections.defaultdict(list)
53+
for s in strs:
54+
k = ''.join(sorted(list(s)))
55+
chars[k].append(s)
56+
return list(chars.values())
4157
```
4258

4359
### **Java**
4460

4561
<!-- 这里可写当前语言的特殊实现逻辑 -->
4662

4763
```java
64+
class Solution {
65+
public List<List<String>> groupAnagrams(String[] strs) {
66+
Map<String, List<String>> chars = new HashMap<>();
67+
for (String s : strs) {
68+
char[] t = s.toCharArray();
69+
Arrays.sort(t);
70+
String k = new String(t);
71+
chars.computeIfAbsent(k, key -> new ArrayList<>()).add(s);
72+
}
73+
return new ArrayList<>(chars.values());
74+
}
75+
}
76+
```
77+
78+
### **C++**
79+
80+
```cpp
81+
class Solution {
82+
public:
83+
vector<vector<string>> groupAnagrams(vector<string> &strs) {
84+
unordered_map<string, vector<string>> chars;
85+
for (auto s : strs)
86+
{
87+
string k = s;
88+
sort(k.begin(), k.end());
89+
chars[k].emplace_back(s);
90+
}
91+
vector<vector<string>> res;
92+
for (auto it = chars.begin(); it != chars.end(); ++it)
93+
{
94+
res.emplace_back(it->second);
95+
}
96+
return res;
97+
}
98+
};
99+
```
48100
101+
### **Go**
102+
103+
```go
104+
func groupAnagrams(strs []string) [][]string {
105+
chars := map[string][]string{}
106+
for _, s := range strs {
107+
key := []byte(s)
108+
sort.Slice(key, func(i, j int) bool {
109+
return key[i] < key[j]
110+
})
111+
chars[string(key)] = append(chars[string(key)], s)
112+
}
113+
var res [][]string
114+
for _, v := range chars {
115+
res = append(res, v)
116+
}
117+
return res
118+
}
49119
```
50120

51121
### **...**

lcci/10.02.Group Anagrams/README_EN.md

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,84 @@
3535

3636
## Solutions
3737

38+
| key | value |
39+
| ------- | ----------------------- |
40+
| `"aet"` | `["eat", "tea", "ate"]` |
41+
| `"ant"` | `["tan", "nat"] ` |
42+
| `"abt"` | `["bat"] ` |
43+
3844
<!-- tabs:start -->
3945

4046
### **Python3**
4147

4248
```python
43-
49+
class Solution:
50+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
51+
chars = collections.defaultdict(list)
52+
for s in strs:
53+
k = ''.join(sorted(list(s)))
54+
chars[k].append(s)
55+
return list(chars.values())
4456
```
4557

4658
### **Java**
4759

4860
```java
61+
class Solution {
62+
public List<List<String>> groupAnagrams(String[] strs) {
63+
Map<String, List<String>> chars = new HashMap<>();
64+
for (String s : strs) {
65+
char[] t = s.toCharArray();
66+
Arrays.sort(t);
67+
String k = new String(t);
68+
chars.computeIfAbsent(k, key -> new ArrayList<>()).add(s);
69+
}
70+
return new ArrayList<>(chars.values());
71+
}
72+
}
73+
```
74+
75+
### **C++**
76+
77+
```cpp
78+
class Solution {
79+
public:
80+
vector<vector<string>> groupAnagrams(vector<string> &strs) {
81+
unordered_map<string, vector<string>> chars;
82+
for (auto s : strs)
83+
{
84+
string k = s;
85+
sort(k.begin(), k.end());
86+
chars[k].emplace_back(s);
87+
}
88+
vector<vector<string>> res;
89+
for (auto it = chars.begin(); it != chars.end(); ++it)
90+
{
91+
res.emplace_back(it->second);
92+
}
93+
return res;
94+
}
95+
};
96+
```
4997
98+
### **Go**
99+
100+
```go
101+
func groupAnagrams(strs []string) [][]string {
102+
chars := map[string][]string{}
103+
for _, s := range strs {
104+
key := []byte(s)
105+
sort.Slice(key, func(i, j int) bool {
106+
return key[i] < key[j]
107+
})
108+
chars[string(key)] = append(chars[string(key)], s)
109+
}
110+
var res [][]string
111+
for _, v := range chars {
112+
res = append(res, v)
113+
}
114+
return res
115+
}
50116
```
51117

52118
### **...**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
vector<vector<string>> groupAnagrams(vector<string> &strs) {
4+
unordered_map<string, vector<string>> chars;
5+
for (auto s : strs)
6+
{
7+
string k = s;
8+
sort(k.begin(), k.end());
9+
chars[k].emplace_back(s);
10+
}
11+
vector<vector<string>> res;
12+
for (auto it = chars.begin(); it != chars.end(); ++it)
13+
{
14+
res.emplace_back(it->second);
15+
}
16+
return res;
17+
}
18+
};

lcci/10.02.Group Anagrams/Solution.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func groupAnagrams(strs []string) [][]string {
2+
chars := map[string][]string{}
3+
for _, s := range strs {
4+
key := []byte(s)
5+
sort.Slice(key, func(i, j int) bool {
6+
return key[i] < key[j]
7+
})
8+
chars[string(key)] = append(chars[string(key)], s)
9+
}
10+
var res [][]string
11+
for _, v := range chars {
12+
res = append(res, v)
13+
}
14+
return res
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public List<List<String>> groupAnagrams(String[] strs) {
3+
Map<String, List<String>> chars = new HashMap<>();
4+
for (String s : strs) {
5+
char[] t = s.toCharArray();
6+
Arrays.sort(t);
7+
String k = new String(t);
8+
chars.computeIfAbsent(k, key -> new ArrayList<>()).add(s);
9+
}
10+
return new ArrayList<>(chars.values());
11+
}
12+
}

lcci/10.02.Group Anagrams/Solution.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
3+
chars = collections.defaultdict(list)
4+
for s in strs:
5+
k = ''.join(sorted(list(s)))
6+
chars[k].append(s)
7+
return list(chars.values())

lcof2/剑指 Offer II 033. 变位词组/README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,7 @@ class Solution {
8585
char[] t = s.toCharArray();
8686
Arrays.sort(t);
8787
String k = new String(t);
88-
if (!chars.containsKey(k)) {
89-
chars.put(k, new ArrayList<>());
90-
}
91-
chars.get(k).add(s);
88+
chars.computeIfAbsent(k, key -> new ArrayList<>()).add(s);
9289
}
9390
return new ArrayList<>(chars.values());
9491
}

lcof2/剑指 Offer II 033. 变位词组/Solution.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ public List<List<String>> groupAnagrams(String[] strs) {
55
char[] t = s.toCharArray();
66
Arrays.sort(t);
77
String k = new String(t);
8-
if (!chars.containsKey(k)) {
9-
chars.put(k, new ArrayList<>());
10-
}
11-
chars.get(k).add(s);
8+
chars.computeIfAbsent(k, key -> new ArrayList<>()).add(s);
129
}
1310
return new ArrayList<>(chars.values());
1411
}

solution/0000-0099/0040.Combination Sum II/Solution.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,24 @@ class Solution {
44

55
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
66
Arrays.sort(candidates);
7-
combinationSum(candidates,target,candidates.length-1, new ArrayList<>());
7+
combinationSum(candidates, target, candidates.length - 1, new ArrayList<>());
88
return result;
99
}
1010

11-
private void combinationSum(int[] candidates, int target,int length, List<Integer> integers) {
11+
private void combinationSum(int[] candidates, int target, int length, List<Integer> integers) {
1212
List<Integer> list;
1313
for (int i = length; i >= 0; i--) {
1414
int nc = candidates[i];
15-
if (nc>target || i<length && nc==candidates[i+1]) continue;
15+
if (nc > target || i < length && nc == candidates[i + 1]) {
16+
continue;
17+
}
1618
list = new ArrayList<>(integers);
1719
list.add(nc);
18-
if (nc==target) result.add(list);
19-
else combinationSum(candidates, target - nc, i-1, list);
20+
if (nc == target) {
21+
result.add(list);
22+
} else {
23+
combinationSum(candidates, target - nc, i - 1, list);
24+
}
2025
}
2126
}
22-
}
27+
}

solution/0000-0099/0049.Group Anagrams/README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@ class Solution {
6767
char[] t = s.toCharArray();
6868
Arrays.sort(t);
6969
String k = new String(t);
70-
if (!chars.containsKey(k)) {
71-
chars.put(k, new ArrayList<>());
72-
}
73-
chars.get(k).add(s);
70+
chars.computeIfAbsent(k, key -> new ArrayList<>()).add(s);
7471
}
7572
return new ArrayList<>(chars.values());
7673
}

0 commit comments

Comments
 (0)