题目链接:https://leetcode.com/problems/maximum-product-subarray/description/

题目大意:给出一串数组,找出连续子数组中乘积最大的子数组的乘积。

法一:暴力。竟然能过,数据也太水了。两个for循环,遍历每一个可能的连续子数组,找出最大值。代码如下(耗时161ms):

 1     public int maxProduct(int[] nums) {
 2         int res = Integer.MIN_VALUE, len = nums.length;
 3         for(int i = 0; i < len; i++) {
 4             int sum = 1;
 5             for(int j = i; j < len; j++) {
 6                 sum *= nums[j];
 7                 res = Math.max(res, sum);
 8             }
 9         }
10         return res;
11     }
View Code

相关文章: