Skip to content

Commit

Permalink
feat: add swift implementation to lcof2 problem: No.075 (#3409)
Browse files Browse the repository at this point in the history
  • Loading branch information
klever34 authored Aug 13, 2024
1 parent 3c456ce commit 8e3db9e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lcof2/剑指 Offer II 075. 数组相对排序/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,38 @@ class Solution:
return arr1
```

#### Swift

```swift
class Solution {
func relativeSortArray(_ arr1: [Int], _ arr2: [Int]) -> [Int] {
var frequency = [Int](repeating: 0, count: 1001)
var result = [Int]()

for num in arr1 {
frequency[num] += 1
}

for num in arr2 {
while frequency[num] > 0 {
result.append(num)
frequency[num] -= 1
}
}

for num in 0..<frequency.count {
while frequency[num] > 0 {
result.append(num)
frequency[num] -= 1
}
}

return result
}
}

```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
26 changes: 26 additions & 0 deletions lcof2/剑指 Offer II 075. 数组相对排序/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
func relativeSortArray(_ arr1: [Int], _ arr2: [Int]) -> [Int] {
var frequency = [Int](repeating: 0, count: 1001)
var result = [Int]()

for num in arr1 {
frequency[num] += 1
}

for num in arr2 {
while frequency[num] > 0 {
result.append(num)
frequency[num] -= 1
}
}

for num in 0..<frequency.count {
while frequency[num] > 0 {
result.append(num)
frequency[num] -= 1
}
}

return result
}
}

0 comments on commit 8e3db9e

Please sign in to comment.