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){ Integer max; Integer endIndex; 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; } }
|