【题目】

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.

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.

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.

【思路】

DP/贪心都可以,贪心更简单。

核心比较A[i+2]-A[i+1]和A[i+1]-A[i]是否异号,是则cnt++,否则i++循环。

两个指针和一个循环,特殊情况只有两个元素的数组单独讨论。

才发现可以插入代码!!好看多了……!

【代码】

class Solution {
public int wiggleMaxLength(int[] nums) {
if (nums.length<2)
return nums.length;
int p1=nums[1]-nums[0];
int cnt=p1!=0?2:1;
for(int i=2;i<nums.length;i++){
int p2=nums[i]-nums[i-1];
if((p1<=0&&p2>0)||(p1>=0&&p2<0)){
p1=p2;
cnt++
;
}
}
return cnt;
}
}

【举例】

Example 1:

Input: [1,7,4,9,2,5]
Output: 6
Explanation: The entire sequence is a wiggle sequence.

Example 2:

Input: [1,17,5,10,13,15,10,5,16,8]
Output: 7
Explanation: There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8].

Example 3:

Input: [1,2,3,4,5,6,7,8,9]
Output: 2

[Leetcode 376]摇摆序列 Wiggle Subsequence的更多相关文章

  1. Java实现 LeetCode 376 摆动序列

    376. 摆动序列 如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列.第一个差(如果存在的话)可能是正数或负数.少于两个元素的序列也是摆动序列. 例如, [1,7,4,9,2,5 ...

  2. [Swift]LeetCode376. 摆动序列 | Wiggle Subsequence

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  3. LeetCode——376.摆动序列

    如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列.第一个差(如果存在的话)可能是正数或负数.少于两个元素的序列也是摆动序列. 例如, [1,7,4,9,2,5] 是一个摆动序列, ...

  4. Leetcode 376.摆动序列

    摆动序列 如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列.第一个差(如果存在的话)可能是正数或负数.少于两个元素的序列也是摆动序列. 例如, [1,7,4,9,2,5] 是一个 ...

  5. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  6. Week13 - 376. Wiggle Subsequence

    Week13 - 376. Wiggle Subsequence A sequence of numbers is called a wiggle sequence if the difference ...

  7. 【LeetCode】NO.376 摆动序列 (Python) [贪心算法]

    376. 摆动序列 题目 如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为 摆动序列 .第一个差(如果存在的话)可能是正数或负数.仅有一个元素或者含两个不等元素的序列也视作摆动序列. 例 ...

  8. 【LeetCode】334. Increasing Triplet Subsequence 解题报告(Python)

    [LeetCode]334. Increasing Triplet Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

  9. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

随机推荐

  1. Hypergeometric distribution

    How TermFinder calculates P-values Readme: MGI GO Term Finder The GoTermFinder attempts to determine ...

  2. 混合线性模型(linear mixed models)

    一般线性模型.混合线性模型.广义线性模型 广义线性模型GLM很简单,举个例子,药物的疗效和服用药物的剂量有关.这个相关性可能是多种多样的,可能是简单线性关系(发烧时吃一片药退烧0.1度,两片药退烧0. ...

  3. Synergy软件的基本配置/使用(详细教程)

    1.Synergy软件的简介 Synergy是一款可让多台电脑共享一个鼠标与键盘的软件,用户可借助Synergy操作一个鼠标与键盘控制多个电脑…… 2.Synergy软件的配置过程 下载链接:计算机相 ...

  4. G711编解码

    http://blog.csdn.net/rightorwrong/article/details/4209467 搞语音对讲几天了,播放时声音干扰太大了.拖得时间久有两个原因: 1.每次采样的位数这 ...

  5. shiro中记住我功能

    Shiro提供了记住我(RememberMe)的功能,比如访问如淘宝等一些网站时,关闭了浏览器下次再打开时还是能记住你是谁,下次访问时无需再登录即可访问,基本流程如下: 1.首先在登录页面选中Reme ...

  6. shiro身份验证

    身份验证,即在应用中谁能证明他就是他本人.一般提供如他们的身份ID一些标识信息来表明他就是他本人,如提供身份证,用户名/密码来证明. 在shiro中,用户需要提供principals (身份)和cre ...

  7. Git:从远程库克隆到本地库及更新本地库

    1.例如我们克隆远程库gitskills 2.克隆 2.1选择合适的地方(文件目录),点击Git Bash Here. 2.2如果是github远程库,进行以下操作: 点击gitskills,再点击红 ...

  8. 基本数据类型int,bool,str

    .基本数据类型(int,bool,str) 基本数据数据类型: int 整数 str 字符串. 一般不存放大量的数据 bool 布尔值. 用来判断. True, False list 列表.用来存放大 ...

  9. 【转】给DataTable和DataRow扩展方法,直接转换为对象集合或对象

    /// <summary> /// 类 说 明:给DataTable和DataRow扩展方法,直接转换为对象集合或对象 /// 补充说明:此扩展类可以极大的简化操作,但是性能低下,大数据以 ...

  10. Python3+BaiduAI识别高颜值妹子图片

    一.在百度云平台创建应用 为什么要到百度云平台创建应用,首先来说是为了获取获取access_token时需要的API Key和Secret Key 至于为什么需要API Key和Secret Key才 ...