File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed
lcof2/剑指 Offer II 033. 变位词组 Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -158,6 +158,26 @@ function groupAnagrams(strs: string[]): string[][] {
158
158
}
159
159
```
160
160
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
+
161
181
<!-- tabs: end -->
162
182
163
183
<!-- solution: end -->
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments