LeetCode知识点总结 - 376
admin
2024-01-19 07:08:10

LeetCode 376. Wiggle Subsequence

考点难度
DPMedium
题目

A wiggle sequence is a sequence where 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 one element and a sequence with two non-equal elements are trivially wiggle sequences.

For example, [1, 7, 4, 9, 2, 5] is a wiggle sequence because the differences (6, -3, 5, -7, 3) alternate between positive and negative.
In contrast, [1, 4, 7, 2, 5] and [1, 7, 4, 5, 5] are not wiggle sequences. The first is not because its first two differences are positive, and the second is not because its last difference is zero.
A subsequence is obtained by deleting some elements (possibly zero) from the original sequence, leaving the remaining elements in their original order.

Given an integer array nums, return the length of the longest wiggle subsequence of nums.

思路

O(n) time, O(1) space

答案
class Solution:def wiggleMaxLength(self, nums):if not nums:return 0length = 1up = None # current is increasing or notfor i in range(1, len(nums)):if nums[i] > nums[i - 1] and up != True:length += 1up = Trueif nums[i] < nums[i - 1] and up != False:length += 1up = Falsereturn length

相关内容

热门资讯

俄媒:俄罗斯来华游客数量稳步增... 参考消息网11月19日报道 据俄罗斯自由媒体网11月17日报道,自9月15日中国对俄罗斯游客试行免签...
“根本不愁卖,来晚了就没了”!... “在网上看到了,特地来尝鲜。”近日,“00后”陈女士打卡了一款名为“奶皮子糖葫芦”的零食。这款零食由...
重磅发布!2026全球高端餐厅... 来源:携程美食林 当用户真实评价 与顶尖美食家的专业洞察交汇 呈现给所有人的不仅是榜单的更迭 更是携...
治愈系美食爱情微短剧《宴遇簪花... 作为泉州成功入选“世界美食之都”后的首部治愈系美食爱情微短剧,《宴遇簪花缘》今日正式公布首轮剧照。首...
【京·花果蜜】怀柔栗子天团,申... 谁能拒绝这一美味?栗子酥、冰栗、栗子蛋糕…个个都是实力派!这口百年老树的甜,你最爱哪一款?