GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — LeetCode/Others/978.Longest-Turbulent-Subarray at master · wisdompeak/LeetCode

Expand file tree

/

978.Longest-Turbulent-Subarray

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

978.Longest-Turbulent-Subarray

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
978.Longest-Turbulent-Subarray.cpp
978.Longest-Turbulent-Subarray.cpp
 
 
Readme.md
Readme.md
 
 

Readme.md

978.Longest-Turbulent-Subarray

将原始数组的相邻两个元素做差,如果为正就标记1,如果为负就标记-1,如果相等就标记0. 那么我们就能得到一个长度为n-1的序列q。显然,我们需要找q中最长的一段subarray,并且元素是1和-1间隔的。一个等价的判断就是q中的相邻元素的乘积是-1.

假设我们从i开始作为起点,那么subarray能变长的条件就是q[i]*q[i+1]==-1,这样我们就递增i,直至可以找到最后一个满足条件的位置i',那么q[i:i']就是一段最长的+/-1相间的序列。注意,q是差分元素,所以实际在nums里,subarray长度就是i'-i+2.

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.