Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ac0a028

Browse files
feat: add swift implementation to lcci problem: No.16.17 (doocs#2748)
1 parent 240d83e commit ac0a028

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

‎lcci/16.17.Contiguous Sequence/README.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,22 @@ var maxSubArray = function (nums) {
112112
};
113113
```
114114

115+
```swift
116+
class Solution {
117+
func maxSubArray(_ nums: [Int]) -> Int {
118+
var ans = Int.min
119+
var f = Int.min
120+
121+
for x in nums {
122+
f = max(f, 0) + x
123+
ans = max(ans, f)
124+
}
125+
126+
return ans
127+
}
128+
}
129+
```
130+
115131
<!-- tabs:end -->
116132

117133
<!-- end -->

‎lcci/16.17.Contiguous Sequence/README_EN.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,22 @@ var maxSubArray = function (nums) {
124124
};
125125
```
126126

127+
```swift
128+
class Solution {
129+
func maxSubArray(_ nums: [Int]) -> Int {
130+
var ans = Int.min
131+
var f = Int.min
132+
133+
for x in nums {
134+
f = max(f, 0) + x
135+
ans = max(ans, f)
136+
}
137+
138+
return ans
139+
}
140+
}
141+
```
142+
127143
<!-- tabs:end -->
128144

129145
<!-- end -->
Lines changed: 13 additions & 0 deletions
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 f = Int.min
5+
6+
for x in nums {
7+
f = max(f, 0) + x
8+
ans = max(ans, f)
9+
}
10+
11+
return ans
12+
}
13+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /