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 ...
随机推荐
- react学习总结
http://www.runoob.com/react/react-tutorial.html一般先看一些中文的简单的介绍和一些基本概念http://reactjs.cn/react/docs/get ...
- 1593: [Usaco2008 Feb]Hotel 旅馆
1593: [Usaco2008 Feb]Hotel 旅馆 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 391 Solved: 228[Submit ...
- java爬虫查找四川大学所有学院的网站的网址中的通知和新闻——以计算机学院为例
需求:查找四川大学所有学院的网站的网址中的通知和新闻——以计算机学院为例 流程图 3. 具体步骤 (1) 学院的主页为:http://cs.scu.edu.cn/ 获取该页面的所有内容(本文只获取新闻 ...
- StudyJams学习历程总结
Study Jams 是一个学习 Google 在线课程的活动.该活动由学员自发组建课程学习小组,旨在带领小组成员入门 Android 开发,最终将 Android App 上载至 Google Pl ...
- spring-AOP-基于Schema切面的小例子
前言: 如果一个项目没有 jdk 5.0 , 那就无法使用基于@AspectJ 注解 的切面. 但是使用AspectJ的表达式的大门还是可以使用的. 我们可以用java提供的Schema配置方法,来替 ...
- ZJOI2017 Day3 滚粗记
私のZJOI Day3 2017-3-21 07:52:53 今天,考了人生当中的第一次省选(虽然只是普及组三等奖但仍然有幸能体会一下).据胡老师说,这就是来体验一下被大神虐--真的是这样,听课听不懂 ...
- JS Math.round()方法原理
请先测试代码: <!doctype html> <html lang="en"> <head> <meta charset="U ...
- flex盒子里面元素linehight对高度的影响
那天,高高兴兴的测试flex属性.大家都知道,当父盒子display:flex的时候,子盒子即使宽高和大于父盒子,也会由于flex的特性进行收缩:然而,在给父盒子加了flex-direction:co ...
- React+Node初尝试
这是第一次写React和Node,选用的是前端Material-ui框架,后端使用的是Express框架,数据库采用的是Mongodb. 项目代码在:GitHub/lilu_movie 这是一个通过从 ...
- 使用Python对Access读写操作
学习Python的过程中,我们会遇到Access的读写问题,这时我们可以利用win32.client模块的COM组件访问功能,通过ADODB操作Access的文件. 1.导入模块 import win ...