当前位置:网站首页>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;
}
}边栏推荐
- [Android -- data storage] use SQLite to store data
- 预测——灰色预测
- The latest interview experience of Android manufacturers in 2022, Android view+handler+binder
- Binary search tree (features)
- 字节跳动Android金三银四解析,android面试题app
- 一文读懂数仓中的pg_stat
- Lie cow count (spring daily question 53)
- 网关Gateway的介绍与使用
- 掌握这个提升路径,面试资料分享
- HAVE FUN | “飞船计划”活动最新进展
猜你喜欢
随机推荐
LeetCode 1043. 分隔数组以得到最大和 每日一题
LeetCode 403. 青蛙过河 每日一题
time标准库
skimage学习(1)
LeetCode 1477. 找两个和为目标值且不重叠的子数组 每日一题
最新Android高级面试题汇总,Android面试题及答案
LeetCode 1986. 完成任务的最少工作时间段 每日一题
QT视频传输
整理几个重要的Android知识,高级Android开发面试题
Binary search tree (features)
Tidb cannot start after modifying the configuration file
Personal notes of graphics (3)
正在准备面试,分享面经
LeetCode 1654. 到家的最少跳跃次数 每日一题
面向接口编程
OpenGL personal notes
QT 图片背景色像素处理法
【Seaborn】组合图表、多子图的实现
预售17.9万,恒驰5能不能火?产品力在线,就看怎么卖
"The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."







![[vulnhub range] thales:1](/img/fb/721d08697afe9b26c94fede628c4d1.png)

