leetcode-1567.乘积为正数的最长子数组长度

原始思路

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
class Solution {
public int getMaxLen(int[] nums) {
return findMax(nums);
}
public static int findMax(int[] nums){
//从第一个位置开始,遇到0了就停止并返回最大的数组乘积的起始与结束位置的index
Integer max;
// 记录此时max为正数的
Integer endIndex;
//初始化最大长度为0
int maxLen = 0;

for (int i =0; i <nums.length; i++){
endIndex = -1;
max = null;

//剪枝
if ((nums.length - i + 1)<=maxLen){
return maxLen;
}

//记录出现负数的次数
for (int j =i; j <nums.length; j++){
if (nums[j] == 0){
//结束循环
break;
}
int num = nums[j] > 0 ? 1 : -1;
if (max == null) {
max = num;
}else {
max = max * num;
}
if (max > 0) {
endIndex = j;
}
}

//记录此时为正数的最大长度
if (endIndex - i + 1 > maxLen){
maxLen = endIndex - i + 1;
}
}

return maxLen;
}
}