leet-code/src/main/java/leetcode/editor/cn/WiggleSubsequence.java
huangge1199@hotmail.com 6aee12ffeb 376:摆动序列
2021-09-08 21:39:46 +08:00

95 lines
2.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为 摆动序列 。第一个差(如果存在的话)可能是正数或负数。仅有一个元素或者含两个不等元素的序列也
//视作摆动序列。
//
//
//
// 例如, [1, 7, 4, 9, 2, 5] 是一个 摆动序列 ,因为差值 (6, -3, 5, -7, 3) 是正负交替出现的。
//
// 相反,[1, 4, 7, 2, 5] 和 [1, 7, 4, 5, 5] 不是摆动序列,第一个序列是因为它的前两个差值都是正数,第二个序列是因为它的最后一
//个差值为零。
//
//
// 子序列 可以通过从原始序列中删除一些(也可以不删除)元素来获得,剩下的元素保持其原始顺序。
//
// 给你一个整数数组 nums ,返回 nums 中作为 摆动序列 的 最长子序列的长度 。
//
//
//
// 示例 1
//
//
//输入nums = [1,7,4,9,2,5]
//输出6
//解释:整个序列均为摆动序列,各元素之间的差值为 (6, -3, 5, -7, 3) 。
//
//
// 示例 2
//
//
//输入nums = [1,17,5,10,13,15,10,5,16,8]
//输出7
//解释:这个序列包含几个长度为 7 摆动序列。
//其中一个是 [1, 17, 10, 13, 10, 16, 8] ,各元素之间的差值为 (16, -7, 3, -3, 6, -8) 。
//
//
// 示例 3
//
//
//输入nums = [1,2,3,4,5,6,7,8,9]
//输出2
//
//
//
//
// 提示:
//
//
// 1 <= nums.length <= 1000
// 0 <= nums[i] <= 1000
//
//
//
//
// 进阶:你能否用 O(n) 时间复杂度完成此题?
// Related Topics 贪心 数组 动态规划 👍 502 👎 0
package leetcode.editor.cn;
//376:摆动序列
class WiggleSubsequence {
public static void main(String[] args) {
//测试代码
Solution solution = new WiggleSubsequence().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int wiggleMaxLength(int[] nums) {
if (nums.length < 1) {
return nums.length;
} else if (nums.length == 2) {
return nums[0] == nums[1] ? 1 : 2;
}
int dValue, ans = 0;
for (int i = 1; i < nums.length; ) {
dValue = nums[i] - nums[i - 1];
i++;
if (dValue > 0) {
ans++;
while (i < nums.length && nums[i - 1] <= nums[i]) {
i++;
}
} else if (dValue < 0) {
ans++;
while (i < nums.length && nums[i - 1] >= nums[i]) {
i++;
}
}
}
return ans + 1;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}