class Solution {
public:
int maxProduct(vector<int>& nums) {
int N = nums.size();
if(N==1) return nums[0];
int cnt = 0;
long long product = 1;
long long ans = 0;
int L = 0;
int R = 0;
for(;R<N;R++){
if(nums[R] == 0){
if( (R-L>1) && (cnt&1) ){
for(;L<R;L++){
product /= nums[L];
if(nums[L]<0) break;
}
ans = max(ans,product);
}
L = R+1;
product = 1;
cnt = 0;
continue;
}
if(nums[R] < 0) cnt++;
product *= nums[R];
ans = max(ans,product);
}
if( (R-L>1) && (cnt&1) ){
for(;L<R;L++){
product /= nums[L];
if(nums[L]<0) break;
}
ans = max(ans,product);
}
return ans;
}
};
No.113 - LeetCode152 - 最大乘积子串
最新推荐文章于 2024-11-16 17:20:24 发布