mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加 0376.摆动序列.md C语言
This commit is contained in:
@ -298,5 +298,32 @@ var wiggleMaxLength = function(nums) {
|
||||
};
|
||||
```
|
||||
|
||||
### C
|
||||
**贪心**
|
||||
```c
|
||||
int wiggleMaxLength(int* nums, int numsSize){
|
||||
if(numsSize <= 1)
|
||||
return numsSize;
|
||||
|
||||
int length = 1;
|
||||
int preDiff , curDiff;
|
||||
preDiff = curDiff = 0;
|
||||
for(int i = 0; i < numsSize - 1; ++i) {
|
||||
// 计算当前i元素与i+1元素差值
|
||||
curDiff = nums[i+1] - nums[i];
|
||||
|
||||
// 若preDiff与curDiff符号不符,则子序列长度+1。更新preDiff的符号
|
||||
// 若preDiff与curDiff符号一致,当前i元素为连续升序/连续降序子序列的中间元素。不被记录入长度
|
||||
// 注:当preDiff为0时,curDiff为正或为负都属于符号不同
|
||||
if((curDiff > 0 && preDiff <= 0) || (preDiff >= 0 && curDiff < 0)) {
|
||||
preDiff = curDiff;
|
||||
length++;
|
||||
}
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
Reference in New Issue
Block a user