File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -375,7 +375,31 @@ function wiggleMaxLength(nums: number[]): number {
375
375
};
376
376
```
377
377
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
+ }
378
398
399
+ result
400
+ }
401
+ }
402
+ ```
379
403
380
404
-----------------------
381
405
<div align =" center " ><img src =https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width =500 > </img ></div >
You can’t perform that action at this time.
0 commit comments