Skip to content

Commit bfb00b9

Browse files
authored
feat: add swift implementation to lcp problem: No.61 (#3932)
1 parent 2dd558c commit bfb00b9

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

lcp/LCP 61. 气温变化趋势/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,31 @@ impl Solution {
183183
}
184184
```
185185

186+
#### Swift
187+
188+
```swift
189+
class Solution {
190+
func temperatureTrend(_ temperatureA: [Int], _ temperatureB: [Int]) -> Int {
191+
var maxTrend = 0
192+
var currentTrend = 0
193+
194+
for i in 0..<temperatureA.count - 1 {
195+
let changeA = temperatureA[i + 1] - temperatureA[i]
196+
let changeB = temperatureB[i + 1] - temperatureB[i]
197+
198+
if (changeA == 0 && changeB == 0) || (changeA * changeB > 0) {
199+
currentTrend += 1
200+
maxTrend = max(maxTrend, currentTrend)
201+
} else {
202+
currentTrend = 0
203+
}
204+
}
205+
206+
return maxTrend
207+
}
208+
}
209+
```
210+
186211
<!-- tabs:end -->
187212

188213
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
func temperatureTrend(_ temperatureA: [Int], _ temperatureB: [Int]) -> Int {
3+
var maxTrend = 0
4+
var currentTrend = 0
5+
6+
for i in 0..<temperatureA.count - 1 {
7+
let changeA = temperatureA[i + 1] - temperatureA[i]
8+
let changeB = temperatureB[i + 1] - temperatureB[i]
9+
10+
if (changeA == 0 && changeB == 0) || (changeA * changeB > 0) {
11+
currentTrend += 1
12+
maxTrend = max(maxTrend, currentTrend)
13+
} else {
14+
currentTrend = 0
15+
}
16+
}
17+
18+
return maxTrend
19+
}
20+
}

0 commit comments

Comments
 (0)