1. 题目原址
https://leetcode.com/problems/maximum-product-subarray/
2. 题目描述
3. 题目大意
给定一个字符串,返回连续子字符串的乘积最大值
4. 解题思路
- 定义一个返回值 max,该值的初始值为 Integer.MIN_VALUE
- 两次for循环,一次从前到后,一次从后到前
- 为什么还要从后向前再遍历一次? 因为对于 [3,-1,4] 这样一个数组,如果只从前往后遍历,那么值应该是3,但是实际应该返回 4 。
- 定义一个临时变量,用来计算当前的元素的累积乘积,如果累积乘积为0,说明当前的元素为0,那么就将临时变量 cur置为 1.
5. AC代码
class Solution {
public int maxProduct(int[] nums) {
if(nums == null || nums.length == 0) return 0;
int cur = 1, max = Integer.MIN_VALUE;