当前位置:网站首页>leetcode 376.摆动序列
leetcode 376.摆动序列
2022-07-21 14:22:00 【进击的code儿】
leetcode 376.摆动序列
一、题目
1.题目描述
如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为 **摆动序列 。**第一个差(如果存在的话)可能是正数或负数。仅有一个元素或者含两个不等元素的序列也视作摆动序列。
- 例如,
[1, 7, 4, 9, 2, 5]
是一个 摆动序列 ,因为差值(6, -3, 5, -7, 3)
是正负交替出现的。 - 相反,
[1, 4, 7, 2, 5]
和[1, 7, 4, 5, 5]
不是摆动序列,第一个序列是因为它的前两个差值都是正数,第二个序列是因为它的最后一个差值为零。
子序列 可以通过从原始序列中删除一些(也可以不删除)元素来获得,剩下的元素保持其原始顺序。
给你一个整数数组 nums
,返回 nums
中作为 摆动序列 的 最长子序列的长度 。
示例 1:
输入:nums = [1,7,4,9,2,5]
输出:6
解释:整个序列均为摆动序列,各元素之间的差值为 (6, -3, 5, -7, 3) 。
示例 2:
输入:nums = [1,17,5,10,13,15,10,5,16,8]
输出:7
解释:这个序列包含几个长度为 7 摆动序列。
其中一个是 [1, 17, 10, 13, 10, 16, 8] ,各元素之间的差值为 (16, -7, 3, -3, 6, -8) 。
示例 3:
输入:nums = [1,2,3,4,5,6,7,8,9]
输出:2
提示:
1 <= nums.length <= 1000
0 <= nums[i] <= 1000
2.基础框架
C++基础框架代码如下:
int wiggleMaxLength(vector<int>& nums) {
}
3.解题思路
- 题目分析
题目目标求数组中最长的摆动序列的长度。
一个元素也可以作为摆动序列,摆动序列最小长度至少为1。
子序列可以通过从原始序列中删除一些(也可以不删除)元素来获得,剩下的元素保持其原始顺序。如果出现连续的正数或负数,可以跳过,直到遇到相差和符号不同,再并入最长子序列的长度。
实现代码:
int wiggleMaxLength(vector<int>& nums) { if (nums.size() == 1) return 1; f[1001]; memset(f, 0, sizeof(1)); f[0] = 1; int i = 1; bool flag; while (i < nums.size() && nums[i] == nums[i - 1]) // 如果前面的元素相同。 { f[i] = f[i - 1]; ++i; } if (i < nums.size()) flag = nums[i] > nums[i - 1] ? true : false; // true表示要升序,false表示要降序 for (; i < nums.size(); i++) { if (nums[i] > nums[i - 1] && true == flag) { f[i] = f[i - 1] + 1; flag = false; } else if (nums[i] < nums[i - 1] && false == flag) { f[i] = f[i - 1] + 1; flag = true; } else // 相等的情况 f[i] = f[i - 1]; } return f[i - 1]; }
边栏推荐
- Goldfish rhca memoirs: cl210 Integrated Identity Management - project organization management + chapter experiment
- 过d盾 jsp webshell+冰蝎免杀马探讨
- 文件操作管理
- Those violations in the store will be punished by the official secondary punishment, the most common four
- Early details about visual studio 2019
- Random talk on DRDS flexible affairs
- 开源进销存系统,10分钟搞定,建议收藏!
- Encryption and decryption of yii2
- Selenium常用实战功能指南
- 数字化转型失败的罪魁祸首是什么?
猜你喜欢
leetcode 1380. 矩阵中的幸运数
An idea of solving agile iteration of desktop application with applet Technology
国家互联网信息办公室对滴滴全球股份有限公司依法作出网络安全审查相关行政处罚的决定
Discussion on passing the d-shield PHP webshell without killing horses
Kyligence 出席华为全球智慧金融峰会,加速拓展全球市场
免杀exe技术探讨
移动研发平台EMAS 3.0全新升级,欢迎登陆阿里云官网搜索EMAS进行体验
Postman - post request application / x-www-from-urlencoded
Idea connects to MySQL database
文件操作管理
随机推荐
D3.js + canvas drawing organization chart
关于Visual Studio 2019的前期详情
爱奇艺抖音和好,微博躺枪?
店铺那些违规会被官方二级处罚,最常见的4种
Selenium common practical function Guide
Control of semiconductor refrigeration and heating based on general single chip microcomputer control of cold and hot head in mobile phone radiator massage instrument
Bypass some dog SQL and XSS
Zabbix3.0 alarm settings
C#. Net sqlserver login function
机房可视化管理标签自动生成
金鱼哥RHCA回忆录:CL210执行镜像操作--自定义磁盘镜像
通信基础设施可视化管理在等保2.0中的作用
leetcode 938. 二叉搜索树的范围和
FileReader
室外资源光纤管理
Internet and transport layer protocol
File operation in C language
Encryption and decryption of yii2
数字化转型失败的罪魁祸首是什么?
The state Internet Information Office made a decision on the administrative punishment related to the network security review of didi Global Co., Ltd. in accordance with the law