作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/longest-turbulent-subarray/

题目描述

A subarray A[i], A[i+1], ..., A[j] of A is said to be turbulent if and only if:

  • For i <= k < j, A[k] > A[k+1] when k is odd, and A[k] < A[k+1] when k is even;
  • OR, for i <= k < j, A[k] > A[k+1] when k is even, and A[k] < A[k+1] when k is odd.
    That is, the subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray.

Return the length of a maximum size turbulent subarray of A.

Example 1:

Input: [9,4,2,10,7,8,8,1,9]
Output: 5
Explanation: (A[1] > A[2] < A[3] > A[4] < A[5])

Example 2:

Input: [4,8,12,16]
Output: 2

Example 3:

Input: [100]
Output: 1

Note:

  1. 1 <= A.length <= 40000
  2. 0 <= A[i] <= 10^9

题目大意

如果相邻的两个数字的差在大于0和小于0这两种情况中互相翻转,那么说明这个子数组是符合要求的。求最长的符合要求的子数组。

解题方法

虫取法

虫取法就是前后指针交替前进的方式。

对于这个题来说,我们先固定左侧的指针left,然后把右侧指针right向右移动,我使用了isde来表示上一次的相邻数字的差的符号,每次把当前的相邻数字符号和上一次的进行判断。需要注意的是题目要求必须翻转,如果连续数字是相等的则不符合要求。

移动的时候需要注意是不是相邻数字的差交替的:

如果是交替的,那么更新现在的相邻数字差的符号并且更新最长子串的长度;

如果不是交替的,那么需要更新left指针,这里的不是交替有两个情况,一是相等,二是连续的递增或者相减。如果right指向的元素是连续两个相等的,那么left指向right当前的位置当做新的起点;如果是A[right - 2],A[right - 1],A[right]三者有序的情况,那么把left更新到right-1的位置,此时right不要向右移动,相当于把left = right - 1当做新的数组起始位置。

举个栗子:

[9,4,2,10,7,8,8,1,9]

9,4,2是单调递减的三个数字,此时应该把left移动到4的位置,right仍然指向2,开始新的遍历过程。

当来回翻转,right到了第二个8的时候,此时子数组是4,2,10,7,8,8,已经不满足翻转条件,此时把left指向right为第二个8的位置,然后开始新的遍历过程。

再举个栗子:

[1,1,1,1,1,1,1]

这样都是连续相等的数值,那么A[right] == A[right - 1]会一直成立,所以每次都把left更新到right的位置,则没有一个构成翻转的条件,res不更新。由此也可见,应该把满足要求的最小的子数组的长度res初始化为1.

c++代码如下:

class Solution {
public:
int maxTurbulenceSize(vector<int>& A) {
const int N = A.size();
if (N == 1) return 1;
int res = 1;
int left = 0, right = 1;
bool isde = false;
while (right < N) {
if (A[right] == A[right - 1]) {
left = right;
right++;
} else if (right - left == 1 || (A[right] - A[right - 1] < 0 != isde)) {
isde = A[right] - A[right - 1] < 0;
res = max(res, right - left + 1);
right ++;
} else {
left = right - 1;
}
}
return res;
}
};

日期

2019 年 1 月 20 日 —— 这次周赛有点简单

【LeetCode】978. Longest Turbulent Subarray 解题报告(C++)的更多相关文章

  1. LeetCode 978. Longest Turbulent Subarray

    原题链接在这里:https://leetcode.com/problems/longest-turbulent-subarray/ 题目: A subarray A[i], A[i+1], ..., ...

  2. leecode 978. Longest Turbulent Subarray(最长连续波动序列,DP or 滚动数组)

    传送门:点我 978. Longest Turbulent Subarray A subarray A[i], A[i+1], ..., A[j] of A is said to be turbule ...

  3. 978. Longest Turbulent Subarray

    A subarray A[i], A[i+1], ..., A[j] of A is said to be turbulent if and only if: For i <= k < j ...

  4. 【LeetCode】Longest Palindromic Substring 解题报告

    DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...

  5. 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)

    [LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...

  6. 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)

    [LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...

  7. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  8. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  9. 【LeetCode】647. Palindromic Substrings 解题报告(Python)

    [LeetCode]647. Palindromic Substrings 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/p ...

随机推荐

  1. python17进程

    import os import time from multiprocessing.dummy import Process def so_sth(name): print("进程名称{} ...

  2. C 语言do while 循环

    do while 循环小练习 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main(void) 5 { 6 int a ...

  3. DNS域名解析全过程

    一张图看懂DNS域名解析全过程   DNS域名解析是互联网上非常重要的一项服务,上网冲浪(还有人在用这个词吗?)伴随着大量DNS服务来支撑,而对于网站运营来说,DNS域名解析的稳定可靠,意味着更多用户 ...

  4. Linux—&#160;查看系统的位数

    [root@zf-test-web01-4 ~]# file /bin/ls           #"/bin/ls" is a binary file /bin/ls: ELF ...

  5. EXCEL-对筛选出(单独手动隐藏行还是在统计范围内)的表格数据进行统计

    =SUBTOTAL(3,A1:A5)  #计算筛选出的表格中A1:A5中有几个值. =SUBTOTAL(3,I71:I21447)  ,在I71:I21447之间计数,会自动略去没有筛选上的隐藏单元格 ...

  6. jsp页面中HTML注释与jsp注释的区别

    jsp页面中HTML注释与jsp注释的区别 HTML注释 html注释是 : HTML注释:参与编译,会生成到源码中. 所以,不能使用html注释EL表达式和JSTL标签库 jsp注释 jsp注释是 ...

  7. 日常Java 2021/11/2

    Java提供了一种对象序列化的机制,该机制中,一个对象可以被表示为一个字节序列,该字节序列包括该对象的数据.有关对象的类型的信息和存储在对象中数据的类型.将序列化对象写入文件之后,可以从文件中读取出来 ...

  8. A Child's History of England.52

    'Arthur,' said the King, with his wicked eyes more on the stone floor than on his nephew, 'will you ...

  9. day08 Nginx模块

    day08 Nginx模块 lnmp架构 l :Linux n :Nginx m :MySQL p :Python/PHP lnmp架构:是最简单的架构 Nginx中的模块(Python模块):前提是 ...

  10. 零基础学习java------23---------动态代理,ip,url案例

    1. 动态代理 2. ip,url案例 给定的access.log是电信运营商的用户上网数据,第一个字段是时间, 第二个字段是ip地址,第三个字段是访问的网站,其他字段可以忽略不计. 第一个字段是网段 ...