Skip to content

Commit 240d83e

Browse files
authored
feat: add swift implementation to lcci problem: No.16.16 (doocs#2747)
1 parent 704aaea commit 240d83e

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

lcci/16.16.Sub Sort/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,34 @@ function subSort(array: number[]): number[] {
146146
}
147147
```
148148

149+
```swift
150+
class Solution {
151+
func subSort(_ array: [Int]) -> [Int] {
152+
let n = array.count
153+
var mi = Int.max, mx = Int.min
154+
var left = -1, right = -1
155+
156+
for i in 0..<n {
157+
if array[i] < mx {
158+
right = i
159+
} else {
160+
mx = array[i]
161+
}
162+
}
163+
164+
for i in stride(from: n - 1, through: 0, by: -1) {
165+
if array[i] > mi {
166+
left = i
167+
} else {
168+
mi = array[i]
169+
}
170+
}
171+
172+
return [left, right]
173+
}
174+
}
175+
```
176+
149177
<!-- tabs:end -->
150178

151179
<!-- end -->

lcci/16.16.Sub Sort/README_EN.md

+28
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,34 @@ function subSort(array: number[]): number[] {
149149
}
150150
```
151151

152+
```swift
153+
class Solution {
154+
func subSort(_ array: [Int]) -> [Int] {
155+
let n = array.count
156+
var mi = Int.max, mx = Int.min
157+
var left = -1, right = -1
158+
159+
for i in 0..<n {
160+
if array[i] < mx {
161+
right = i
162+
} else {
163+
mx = array[i]
164+
}
165+
}
166+
167+
for i in stride(from: n - 1, through: 0, by: -1) {
168+
if array[i] > mi {
169+
left = i
170+
} else {
171+
mi = array[i]
172+
}
173+
}
174+
175+
return [left, right]
176+
}
177+
}
178+
```
179+
152180
<!-- tabs:end -->
153181

154182
<!-- end -->

lcci/16.16.Sub Sort/Solution.swift

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
func subSort(_ array: [Int]) -> [Int] {
3+
let n = array.count
4+
var mi = Int.max, mx = Int.min
5+
var left = -1, right = -1
6+
7+
for i in 0..<n {
8+
if array[i] < mx {
9+
right = i
10+
} else {
11+
mx = array[i]
12+
}
13+
}
14+
15+
for i in stride(from: n - 1, through: 0, by: -1) {
16+
if array[i] > mi {
17+
left = i
18+
} else {
19+
mi = array[i]
20+
}
21+
}
22+
23+
return [left, right]
24+
}
25+
}

0 commit comments

Comments
 (0)