Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 6427de9

Browse files
aQuaaQua
authored andcommitted
376 added
1 parent 0041ac2 commit 6427de9

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# [376. Wiggle Subsequence](https://leetcode.com/problems/wiggle-subsequence/)
2+
3+
## 题目
4+
5+
A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a wiggle sequence.
6+
7+
For example, [1,7,4,9,2,5] is a wiggle sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, [1,4,7,2,5] and [1,7,4,5,5] are not wiggle sequences, the first because its first two differences are positive and the second because its last difference is zero.
8+
9+
Given a sequence of integers, return the length of the longest subsequence that is a wiggle sequence. A subsequence is obtained by deleting some number of elements (eventually, also zero) from the original sequence, leaving the remaining elements in their original order.
10+
11+
Examples:
12+
13+
```text
14+
Input: [1,7,4,9,2,5]
15+
Output: 6
16+
The entire sequence is a wiggle sequence.
17+
18+
Input: [1,17,5,10,13,15,10,5,16,8]
19+
Output: 7
20+
There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8].
21+
22+
Input: [1,2,3,4,5,6,7,8,9]
23+
Output: 2
24+
```
25+
26+
Follow up:
27+
Can you do it in O(n) time?
28+
29+
Credits:Special thanks to @agave and @StefanPochmann for adding this problem and creating all test cases.
30+
31+
## 解题思路
32+
33+
见程序注释
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package Problem0376
2+
3+
func wiggleMaxLength(nums []int) int {
4+
res :=
5+
6+
return res
7+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package Problem0376
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// tcs is testcase slice
11+
var tcs = []struct {
12+
nums []int
13+
ans int
14+
}{
15+
16+
{
17+
[]int{1, 7, 4, 9, 2, 5},
18+
6,
19+
},
20+
21+
{
22+
[]int{1, 17, 5, 10, 13, 15, 10, 5, 16, 8},
23+
7,
24+
},
25+
26+
{
27+
[]int{1, 2, 3, 4, 5, 6, 7, 8, 9},
28+
2,
29+
},
30+
31+
// 可以有多个 testcase
32+
}
33+
34+
func Test_wiggleMaxLength(t *testing.T) {
35+
ast := assert.New(t)
36+
37+
for _, tc := range tcs {
38+
fmt.Printf("~~%v~~\n", tc)
39+
ast.Equal(tc.ans, wiggleMaxLength(tc.nums), "输入:%v", tc)
40+
}
41+
}
42+
43+
func Benchmark_wiggleMaxLength(b *testing.B) {
44+
for i := 0; i < b.N; i++ {
45+
for _, tc := range tcs {
46+
wiggleMaxLength(tc.nums)
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)