File tree Expand file tree Collapse file tree 2 files changed +58
-0
lines changed
lcof2/剑指 Offer II 075. 数组相对排序 Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change @@ -167,6 +167,38 @@ class Solution:
167
167
return arr1
168
168
```
169
169
170
+ #### Swift
171
+
172
+ ``` swift
173
+ class Solution {
174
+ func relativeSortArray (_ arr1 : [Int ], _ arr2 : [Int ]) -> [Int ] {
175
+ var frequency = [Int ](repeating : 0 , count : 1001 )
176
+ var result = [Int ]()
177
+
178
+ for num in arr1 {
179
+ frequency[num] += 1
180
+ }
181
+
182
+ for num in arr2 {
183
+ while frequency[num] > 0 {
184
+ result.append (num)
185
+ frequency[num] -= 1
186
+ }
187
+ }
188
+
189
+ for num in 0 ..< frequency.count {
190
+ while frequency[num] > 0 {
191
+ result.append (num)
192
+ frequency[num] -= 1
193
+ }
194
+ }
195
+
196
+ return result
197
+ }
198
+ }
199
+
200
+ ```
201
+
170
202
<!-- tabs: end -->
171
203
172
204
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func relativeSortArray( _ arr1: [ Int ] , _ arr2: [ Int ] ) -> [ Int ] {
3
+ var frequency = [ Int] ( repeating: 0 , count: 1001 )
4
+ var result = [ Int] ( )
5
+
6
+ for num in arr1 {
7
+ frequency [ num] += 1
8
+ }
9
+
10
+ for num in arr2 {
11
+ while frequency [ num] > 0 {
12
+ result. append ( num)
13
+ frequency [ num] -= 1
14
+ }
15
+ }
16
+
17
+ for num in 0 ..< frequency. count {
18
+ while frequency [ num] > 0 {
19
+ result. append ( num)
20
+ frequency [ num] -= 1
21
+ }
22
+ }
23
+
24
+ return result
25
+ }
26
+ }
You can’t perform that action at this time.
0 commit comments