Skip to content

Commit 72997fd

Browse files
authored
feat: add swift implementation to lcci problem: No.10.01(#2711)
1 parent 2f4adee commit 72997fd

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

lcci/10.01.Sorted Merge/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,23 @@ var merge = function (A, m, B, n) {
145145
};
146146
```
147147

148+
```swift
149+
class Solution {
150+
func merge(_ A: inout [Int], _ m: Int, _ B: [Int], _ n: Int) {
151+
var i = m - 1, j = n - 1
152+
for k in stride(from: m + n - 1, through: 0, by: -1) {
153+
if j < 0 || (i >= 0 && A[i] > B[j]) {
154+
A[k] = A[i]
155+
i -= 1
156+
} else {
157+
A[k] = B[j]
158+
j -= 1
159+
}
160+
}
161+
}
162+
}
163+
```
164+
148165
<!-- tabs:end -->
149166

150167
<!-- end -->

lcci/10.01.Sorted Merge/README_EN.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,23 @@ var merge = function (A, m, B, n) {
144144
};
145145
```
146146

147+
```swift
148+
class Solution {
149+
func merge(_ A: inout [Int], _ m: Int, _ B: [Int], _ n: Int) {
150+
var i = m - 1, j = n - 1
151+
for k in stride(from: m + n - 1, through: 0, by: -1) {
152+
if j < 0 || (i >= 0 && A[i] > B[j]) {
153+
A[k] = A[i]
154+
i -= 1
155+
} else {
156+
A[k] = B[j]
157+
j -= 1
158+
}
159+
}
160+
}
161+
}
162+
```
163+
147164
<!-- tabs:end -->
148165

149166
<!-- end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
func merge(_ A: inout [Int], _ m: Int, _ B: [Int], _ n: Int) {
3+
var i = m - 1, j = n - 1
4+
for k in stride(from: m + n - 1, through: 0, by: -1) {
5+
if j < 0 || (i >= 0 && A[i] > B[j]) {
6+
A[k] = A[i]
7+
i -= 1
8+
} else {
9+
A[k] = B[j]
10+
j -= 1
11+
}
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)