Skip to content

Commit 83a941f

Browse files
authored
feat: add swift implementation to lcof problem: No.42 (doocs#2923)
1 parent 4973492 commit 83a941f

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lcof/面试题42. 连续子数组的最大和/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,24 @@ public class Solution {
189189
}
190190
```
191191

192+
#### Swift
193+
194+
```swift
195+
class Solution {
196+
func maxSubArray(_ nums: [Int]) -> Int {
197+
var ans = Int.min
198+
var currentSum = 0
199+
200+
for x in nums {
201+
currentSum = max(currentSum, 0) + x
202+
ans = max(ans, currentSum)
203+
}
204+
205+
return ans
206+
}
207+
}
208+
```
209+
192210
<!-- tabs:end -->
193211

194212
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
func maxSubArray(_ nums: [Int]) -> Int {
3+
var ans = Int.min
4+
var currentSum = 0
5+
6+
for x in nums {
7+
currentSum = max(currentSum, 0) + x
8+
ans = max(ans, currentSum)
9+
}
10+
11+
return ans
12+
}
13+
}

0 commit comments

Comments
 (0)