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 5436b82

Browse files
feat: add swift implementation to lcof2 problem: No.033 (doocs#3038)
1 parent 5136f25 commit 5436b82

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,26 @@ function groupAnagrams(strs: string[]): string[][] {
158158
}
159159
```
160160

161+
#### Swift
162+
163+
```swift
164+
class Solution {
165+
func groupAnagrams(_ strs: [String]) -> [[String]] {
166+
var d = [String: [String]]()
167+
168+
for s in strs {
169+
let sortedStr = String(s.sorted())
170+
if d[sortedStr] == nil {
171+
d[sortedStr] = [String]()
172+
}
173+
d[sortedStr]!.append(s)
174+
}
175+
176+
return Array(d.values)
177+
}
178+
}
179+
```
180+
161181
<!-- tabs:end -->
162182

163183
<!-- solution:end -->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
func groupAnagrams(_ strs: [String]) -> [[String]] {
3+
var d = [String: [String]]()
4+
5+
for s in strs {
6+
let sortedStr = String(s.sorted())
7+
if d[sortedStr] == nil {
8+
d[sortedStr] = [String]()
9+
}
10+
d[sortedStr]!.append(s)
11+
}
12+
13+
return Array(d.values)
14+
}
15+
}

0 commit comments

Comments
(0)

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