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

[pull] master from youngyangyang04:master #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 7 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Sep 8, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
更新376.摆动序列 贪心算法Go语言版本
  • Loading branch information
0407-zh committed Sep 5, 2022
commit 8329c3d6d14c16630d6c8a8303ed7fab4080d02f
23 changes: 14 additions & 9 deletions problems/0376.摆动序列.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -269,18 +269,23 @@ class Solution:
**贪心**
```golang
func wiggleMaxLength(nums []int) int {
var count, preDiff, curDiff int //初始化默认为0
count = 1 // 初始化为1,因为最小的序列是1个数
if len(nums) < 2 {
return count
n := len(nums)
if n < 2 {
return n
}
ans := 1
prevDiff := nums[1] - nums[0]
if prevDiff != 0 {
ans = 2
}
for i := 0; i < len(nums)-1; i++ {
curDiff = nums[i+1] - nums[i]
if (curDiff > 0 && preDiff <= 0) || (curDiff < 0 && preDiff >= 0) {
count++
for i := 2; i < n; i++ {
diff := nums[i] - nums[i-1]
if diff > 0 && prevDiff <= 0 || diff < 0 && prevDiff >= 0 {
ans++
prevDiff = diff
}
}
return count
return ans
}
```

Expand Down

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