File tree 3 files changed +45
-0
lines changed
lcci/16.17.Contiguous Sequence
3 files changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -112,6 +112,22 @@ var maxSubArray = function (nums) {
112
112
};
113
113
```
114
114
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
+
115
131
<!-- tabs: end -->
116
132
117
133
<!-- end -->
Original file line number Diff line number Diff line change @@ -124,6 +124,22 @@ var maxSubArray = function (nums) {
124
124
};
125
125
```
126
126
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
+
127
143
<!-- tabs: end -->
128
144
129
145
<!-- end -->
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments