当前位置:网站首页>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;
}
}
边栏推荐
- Prometheus API deletes all data of a specified job
- Lie cow count (spring daily question 53)
- Set the route and optimize the URL in thinkphp3.2.3
- Cesium (4): the reason why gltf model is very dark after loading
- 01tire+ chain forward star +dfs+ greedy exercise one
- Interface oriented programming
- Module VI
- PHP realizes wechat applet face recognition and face brushing login function
- Three. JS series (1): API structure diagram-1
- 直接上干货,100%好评
猜你喜欢
Imitate the choice of enterprise wechat conference room
spark调优(三):持久化减少二次查询
最新2022年Android大厂面试经验,安卓View+Handler+Binder
Personal notes of graphics (4)
Three. JS series (2): API structure diagram-2
Module VI
skimage学习(1)
【DesignMode】代理模式(proxy pattern)
直接上干货,100%好评
Spark Tuning (III): persistence reduces secondary queries
随机推荐
Advanced C language -- function pointer
QT中自定义控件的创建到封装到工具栏过程(一):自定义控件的创建
[C language] question set of X
Deep listening array deep listening watch
spark调优(三):持久化减少二次查询
Find tags in prefab in unity editing mode
[PHP] PHP interface inheritance and interface multi inheritance principle and implementation method
Binary search tree (features)
ByteDance Android gold, silver and four analysis, Android interview question app
使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑
Usage of config in laravel
如何快速检查钢网开口面积比是否符合 IPC7525
Set the route and optimize the URL in thinkphp3.2.3
node:504报错
Horizontal and vertical centering method and compatibility
LeetCode 1155. 掷骰子的N种方法 每日一题
Personal notes of graphics (3)
The difference and working principle between compiler and interpreter
PHP realizes wechat applet face recognition and face brushing login function
AutoLISP series (3): function function 3