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 7fd311f

Browse files
committed
添加 0376.摆动序列.md Scala版本
1 parent 18f29ef commit 7fd311f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

‎problems/0376.摆动序列.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,31 @@ function wiggleMaxLength(nums: number[]): number {
375375
};
376376
```
377377

378+
### Scala
379+
380+
```scala
381+
object Solution {
382+
def wiggleMaxLength(nums: Array[Int]): Int = {
383+
if (nums.length <= 1) return nums.length
384+
var result = 1
385+
var curDiff = 0 // 当前一对的差值
386+
var preDiff = 0 // 前一对的差值
387+
388+
for (i <- 1 until nums.length) {
389+
curDiff = nums(i) - nums(i - 1) // 计算当前这一对的差值
390+
// 当 curDiff > 0 的情况,preDiff <= 0
391+
// 当 curDiff < 0 的情况,preDiff >= 0
392+
// 这两种情况算是两个峰值
393+
if ((curDiff > 0 && preDiff <= 0) || (curDiff < 0 && preDiff >= 0)) {
394+
result += 1 // 结果集加 1
395+
preDiff = curDiff // 当前差值赋值给上一轮
396+
}
397+
}
378398

399+
result
400+
}
401+
}
402+
```
379403

380404
-----------------------
381405
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

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