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/code/lc978.java at master · mJackie/leetcode

Expand file tree

/

lc978.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
51 lines (50 loc) · 1.55 KB
/

lc978.java

Copy path

File metadata and controls

51 lines (50 loc) · 1.55 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package code;
/*
* 978. Longest Turbulent Subarray
* 题意:小,大,小,大 这种最大长度是多少
* 难度:Medium
* 分类:Array, Dynamic Porgramming, Sliding Windows
* 思路:O(n) 遍历一遍就能解决,不用dp
* Tips:lc53
*/
public class lc978 {
public int maxTurbulenceSize(int[] A) { //太难想了
int inc = 1, dec = 1, result = 1;
for (int i = 1; i < A.length; i++) {
if (A[i] < A[i - 1]) { // +1 并且重置另一个统计量
dec = inc + 1;
inc = 1;
} else if (A[i] > A[i - 1]) {
inc = dec + 1;
dec = 1;
} else {
inc = 1;
dec = 1;
}
result = Math.max(result, Math.max(dec, inc));
}
return result;
}
public int maxTurbulenceSize2(int[] A) {
int[] arr = new int[A.length-1];
for(int i=1; i<A.length; i++){ //转换成0,1数组
if(A[i]==A[i-1]) arr[i-1] = 0;
else if(A[i]>A[i-1]) arr[i-1] = 1;
else arr[i-1] = -1;
}
int res = 1;
boolean flag = false; //判断下是否全是0, 返回的时候就不+1
int count = 1;
for(int i=1; i<arr.length; i++){
if(arr[i]!=0) flag=true;
if(arr[i]==-arr[i-1] && arr[i]!=0) {
count++;
}else{
res = Math.max(res, count);
count = 1;
}
}
res = Math.max(res, count);
return flag? res+1: res;
}
}
You are visitor 01402775 — 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.