[Leetcode 376]摇摆序列 Wiggle Subsequence
【题目】
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的更多相关文章
- Java实现 LeetCode 376 摆动序列
376. 摆动序列 如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列.第一个差(如果存在的话)可能是正数或负数.少于两个元素的序列也是摆动序列. 例如, [1,7,4,9,2,5 ...
- [Swift]LeetCode376. 摆动序列 | Wiggle Subsequence
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- LeetCode——376.摆动序列
如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列.第一个差(如果存在的话)可能是正数或负数.少于两个元素的序列也是摆动序列. 例如, [1,7,4,9,2,5] 是一个摆动序列, ...
- Leetcode 376.摆动序列
摆动序列 如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列.第一个差(如果存在的话)可能是正数或负数.少于两个元素的序列也是摆动序列. 例如, [1,7,4,9,2,5] 是一个 ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- Week13 - 376. Wiggle Subsequence
Week13 - 376. Wiggle Subsequence A sequence of numbers is called a wiggle sequence if the difference ...
- 【LeetCode】NO.376 摆动序列 (Python) [贪心算法]
376. 摆动序列 题目 如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为 摆动序列 .第一个差(如果存在的话)可能是正数或负数.仅有一个元素或者含两个不等元素的序列也视作摆动序列. 例 ...
- 【LeetCode】334. Increasing Triplet Subsequence 解题报告(Python)
[LeetCode]334. Increasing Triplet Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
随机推荐
- JavaScript学习第一天(一)
JavaScript介绍 JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本 ...
- POP3、SMTP和IMAP介绍和设置
什么是POP3.SMTP和IMAP? 参照:http://help.163.com/09/1223/14/5R7P6CJ600753VB8.html 用于 Outlook 的 POP 和 IMAP 电 ...
- ThinkPHP 日志(如何学好一门技术,教学视频和文档的优缺)
ThinkPHP 日志(如何学好一门技术,教学视频和文档的优缺) 一.总结 一句话总结:教学视频中介绍的只是基础的最常用的使用,那些不常用的那些视频里面都不会介绍,因为需求小,所以还是需要好好去把参考 ...
- 【转】 g++编译时对'xxxx'未定义的引用问题(undefined reference to)
转自:https://blog.csdn.net/killwho/article/details/53785910 引用:http://www.linuxdiyf.com/linux/16754.ht ...
- javascript ----> Immediately-Invoked Function Expression (IIFE)(翻译)
http://benalman.com/news/2010/11/immediately-invoked-function-expression/ 如果你没有注意到,我对术语有一点点坚持. 所以,在听 ...
- linux重启后进入了救援模式,无法远程登录
卸载删除了某个文件系统分区的逻辑卷,但是没有修改fstab文件,导致系统重启异常,此时已经无法远程连接了,只能本地登录到救援模式修改
- sgu 154
Factorial 题意:能否找到一个数,它的阶乘后面0的个数为n? 数越大,阶乘后的0越多.用二分找.对于一个数x,它的阶乘,将小于等于它的数分解质因数.其中2的个数一定大于5的个数.因此计5的个数 ...
- 关于新手用java写题目,遇到的字符和字符串问题
我看到一遍很好的博客: https://blog.csdn.net/qq_37267015/article/details/78738512 1.首先了,java之中,没有像C语言那样的getchar ...
- CF-721C DAG图拓扑排序+费用DP
比赛的时候写了个记忆化搜索,超时了. 后来学习了一下,这种题目应该用拓扑排序+DP来做. dp[][]保存走到[第i个节点][走过j个点]时所用的最短时间. pre[][]用前驱节点求路径 然后遍历一 ...
- PHP中工厂模式与策略模式区别
策略模式需要自己动手去做,工厂模式是都准备好了你需要选择 工厂模式:有一天你决定去吃披萨,一看菜单,哦,种类很多呀,你就点了个培根披萨,过了二十分钟,你的披萨就来了就可以吃到了.但这个披萨是怎么做的, ...