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.
题目讲解
题目叫做摆动子序列,顾名思义就是不断摆动的的点,不能连续上升也不能连续下降,只能一上一下不断摆动,接着我们要知道子序列对于原序列来说虽然不要求连续但是要求保序(子串、子数组要求连续),所以这里也就没必要排序了。
示例
Input: [1,7,4,9,2,5]
Output: 6
The entire sequence is a wiggle sequence. Input: [1,17,5,10,13,15,10,5,16,8]
Output: 7
There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8]. Input: [1,2,3,4,5,6,7,8,9]
Output: 2
思路及代码
"""
这题的思路其实很简单:
当三个点事连续上升(下降)时,去掉中间的那个点,因为去掉第一个点会影响之前的结果,去掉最后一个点遇到下降的可能性相对较低。 如果相乘为零,则代表是折线点,那么结果加一,判断点向后推一位 下面的代码最费时的地方是相乘的时候,可以优化 """
class Solution(object):
def wiggleMaxLength(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# 边界条件
if len(nums) <= 1:
return len(nums)
ans = 1
# 这里判断有上下折线的方法是求导,相乘为负即为不同
diff = nums[0] - nums[1]
if diff != 0:
ans += 1
for i in range(2,len(nums)):
if nums[i] != nums[i - 1]: # 这里主要是处理相等值的问题
if diff * (nums[i - 1] - nums[i]) <= 0: # 这里的=0主要是为了处理前面都是相等项到起伏点的情况
ans += 1
diff = nums[i - 1] - nums[i] return ans
LeetCode 376. Wiggle Subsequence 摆动子序列的更多相关文章
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- Leetcode 376. Wiggle Subsequence
本题要求在O(n)时间内求解.用delta储存相邻两个数的差,如果相邻的两个delta不同负号,那么说明子序列摇摆了一次.参看下图的nums的plot.这个例子的答案是7.平的线段部分我们支取最左边的 ...
- 376 Wiggle Subsequence 摆动序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- 【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】376. Wiggle Subsequence
Description: A sequence of numbers is called a wiggle sequence if the differences between successive ...
- [LeetCode] 280. Wiggle Sort 摆动排序
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- 376. Wiggle Subsequence
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- [leetcode]392. Is Subsequence 验证子序列
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...
随机推荐
- 购买的wemall 6.0商城系统源码分享
使用方法 1.解压目录 2.cd wemall6 && npm i 3.配置config下的config.json 4.npm start 摒弃以往的开发框架thinkphp,使用no ...
- canvas作图
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 计算两个YUV420P像素数据的PSNR---高等算法
PSNR是最基本的视频质量评价方法.本程序中的函数可以对比两张YUV图片中亮度分量Y的PSNR.函数的代码如下所示. /** * Calculate PSNR between 2 YUV420P fi ...
- 2017年你需要一个VPN
还有29天就2017年了,今天跟同事还在讨论这个问题,2016你都做了些什么事情?2017你有什么计划和期待?有空我们一起来聊聊,今天我们就先来聊聊VPN. 记得2016年11月初的时候,我写过一篇文 ...
- 关于web测试收集
页面部分 页面清单是否完整(是否已经将所需要的页面全部都列出来了) 页面是否显示(在不同分辨率下页面是否存在,在不同浏览器版本中页面是是否显示) 页面在窗口中的显示是否正确.美观(在调整浏览器窗口大小 ...
- mvp架构解析
MVP现在已经是目前最火的架构,很多的框架都是以MVP为基础,甚至于Google自己都出一个MVP的开源架构.https://github.com/googlesamples/android-arch ...
- DAX基础入门 - 30分钟从SQL到DAX -- PowerBI 利器
看到漂漂亮亮的PowerBI报表,手痒痒怎么办?! 有没有面对着稀奇古怪的DAX而感到有点丈八金刚摸不着头脑或者干瞪眼?! 有没有想得到某个值想不出来DAX怎么写而直跳脚!? 看完这篇文章,你会恍然大 ...
- 求一个int型整数的两种递减数之和(java)--2015华为机试题
题目描述: 给出一个整数(负数使用其绝对值),输出这个整数中的两种递减数(1.最大递减数:2.递减数中各位数之和最大的数)之和. 递减数:一个数字的递减数是指相邻的数位从大到小排列的数字,不包含相邻的 ...
- Java完成简单猜数字游戏v2.0
猜数字游戏v2.0 优化了获取随机数.输入数据超出边界值的代码,并增加了异常处理,能够在玩家输入错误数据错误时给出可靠指引,希望对和我一样的新人有帮助, 最后希望有大神愿意帮我解决代码优化的问题,谢谢 ...
- 核心模块Path
核心模块Path 作用:用于帮助程序员来操作硬盘上的路径. 核心模块注意点:当引用核心模块的时候直接require('模块名'),不需要加任何路径或者后缀. Path中的常用API: dirname( ...