Skip to content

Commit 3a84536

Browse files
authored
feat: add swift implementation to lcof problem: No.63 (doocs#2958)
1 parent 5365e1e commit 3a84536

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lcof/面试题63. 股票的最大利润/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,24 @@ public class Solution {
174174
}
175175
```
176176

177+
#### Swift
178+
179+
```swift
180+
class Solution {
181+
func maxProfit(_ prices: [Int]) -> Int {
182+
var mi = Int.max
183+
var ans = 0
184+
185+
for x in prices {
186+
ans = max(ans, x - mi)
187+
mi = min(mi, x)
188+
}
189+
190+
return ans
191+
}
192+
}
193+
```
194+
177195
<!-- tabs:end -->
178196

179197
<!-- solution:end -->
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
func maxProfit(_ prices: [Int]) -> Int {
3+
var mi = Int.max
4+
var ans = 0
5+
6+
for x in prices {
7+
ans = max(ans, x - mi)
8+
mi = min(mi, x)
9+
}
10+
11+
return ans
12+
}
13+
}

0 commit comments

Comments
 (0)