当前位置:网站首页>LeetCode 152. Product maximum subarray daily question
LeetCode 152. Product maximum subarray daily question
2022-07-07 16:58:00 【@Little safflower】
Problem description
Give you an array of integers nums , Please find the non empty continuous subarray with the largest product in the array ( The subarray contains at least one number ), And returns the product of the subarray .
The answer to the test case is 32- position Integers .
Subarray Is a continuous subsequence of an array .
Example 1:
Input : nums = [2,3,-2,4]
Output : 6
explain : Subarray [2,3] There is a maximum product 6.
Example 2:Input : nums = [-2,0,-1]
Output : 0
explain : The result can't be 2, because [-2,-1] It's not a subarray .
Tips :
1 <= nums.length <= 2 * 104
-10 <= nums[i] <= 10
nums The product of any prefix or suffix of Guarantee It's a 32- position Integerssource : Power button (LeetCode)
link :https://leetcode.cn/problems/maximum-product-subarray
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Java
class Solution {
public int maxProduct(int[] nums) {
if(nums.length == 1) return nums[0];
int ans = 0;
int positive = 1;
int negative = 1;
for(int n : nums){
if(positive <= 0) positive = 1;
if(negative >= 0) negative = 1;
if(n < 0){
int t = negative;
negative = positive * n;
positive = t * n;
}else {
negative *= n;
positive *= n;
}
if(positive > ans) ans = positive;
}
return ans;
}
}边栏推荐
猜你喜欢

最新阿里P7技术体系,妈妈再也不用担心我找工作了

Module VI
直接上干货,100%好评

模块六

A tour of gRPC:03 - proto序列化/反序列化

Binary search tree (features)

Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions

【MySql进阶】索引详解(一):索引数据页结构

Three. JS series (2): API structure diagram-2

AutoLISP series (3): function function 3
随机推荐
【Seaborn】组合图表、多子图的实现
LeetCode 1049. 最后一块石头的重量 II 每日一题
C语言进阶——函数指针
Introduction and use of gateway
Geoserver2.18 series (5): connect to SQLSERVER database
LeetCode 1696. 跳跃游戏 VI 每日一题
Ray and OBB intersection detection
模拟Servlet的本质
Personal notes of graphics (1)
[C language] question set of X
LeetCode 1981. 最小化目标值与所选元素的差 每日一题
AutoLISP series (2): function function 2
运算符
Three. JS series (1): API structure diagram-1
Pycharm terminal enables virtual environment
LeetCode 300. 最长递增子序列 每日一题
As an Android Developer programmer, Android advanced interview
预售17.9万,恒驰5能不能火?产品力在线,就看怎么卖
浅浅理解.net core的路由
【医学分割】attention-unet