1. 程式人生 > >152. Maximum Product Subarray​​​​​​​

152. Maximum Product Subarray​​​​​​​

思路一:暴力。不同起點和終點的乘積挨個計算比較。

class Solution {
    public int maxProduct(int[] nums) {
        int max_pro=Integer.MIN_VALUE;
        int temp_pro=1;
        for(int i=0;i<=nums.length-1;i++) {
        	temp_pro=1;
        	for(int j=i;j>=0;j--) {
        		temp_pro*=nums[j];
        	max_pro=Math.max(max_pro, temp_pro);}
        }
        return max_pro;
    }
}

思路二:使用兩個臨時變數,分別儲存前i項乘積的最大值和最小值,因為後面的乘積大小隻受這兩個值影響。

時間複雜度為O(n),遍歷一次後便可求出最大值。

class Solution {
    public int maxProduct(int[] nums) {
        int r=nums[0];
        for(int i=1,imax=r,imin=r;i<=nums.length-1;i++) {
        	if(nums[i]<0) {
        		int temp=imax;
        		imax=imin;
        		imin=temp;
        	}
        	imax=Math.max(imax*nums[i],nums[i]);
        	imin=Math.min(imin*nums[i],nums[i]);
        	r=Math.max(r,imax);
        }
        return r;
    }
}

相關推薦

leetcode 152. Maximum Product Subarray

style 最小數 int least 求最大子數組 ast fin urn bsp leetcode 152. Maximum Product Subarray Find the contiguous subarray within an array (contain

leetcode 152. Maximum Product Subarray 最大連乘子序列

maximum subarray leetcode cxf sub gin max pro rod 灼op胃o躥鐐儷8eukahttp://www.docin.com/app/user/userinfo?userid=178504825 賂u勤蠢40訝m摯4iyoehttp

152 Maximum Product Subarray 乘積最大子序列

-s true www. com for pre pro return ems 找出一個序列中乘積最大的連續子序列(該序列至少包含一個數)。例如, 給定序列 [2,3,-2,4],其中乘積最大的子序列為 [2,3] 其乘積為 6。詳見:https://leetcode.co

152. Maximum Product Subarray 解題記錄

排序 bool fin 第一次 解題思路 擴大 可能 因此 font 題目描述: Find the contiguous subarray within an array (containing at least one number) which has the larg

(Java) LeetCode 152. Maximum Product Subarray —— 乘積最大子序列

ann solution least posit 當前 res 暴力 根據 with Given an integer array nums, find the contiguous subarray within an array (containing at least

[LeetCode] 152. Maximum Product Subarray 求最大子數組乘積

lan range ++i logs local i++ www. spl 題目 Given an integer array nums, find the contiguous subarray within an array (containing at least o

152. Maximum Product Subarray

求一個 最小 最小乘積 div i++ public pro return bubuko 一、題目   1、審題      2、分析     求一個整數數組中的連續子串的最大乘積。 二、解答   1、思路:       ①、遍歷數組,采用三個變量進行記錄。       

[LeetCode] 628. Maximum Product of Three Numbers 三個數字的最大乘積 [LeetCode] 152. Maximum Product Subarray 求最大子陣列乘積 All LeetCode Questions List 題目彙總

Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6  Example 2

#Leetcode# 152. Maximum Product Subarray

https://leetcode.com/problems/maximum-product-subarray/   Given an integer array nums, find the contiguous subarray within an array (containing

Leetcode 152. Maximum Product Subarray (最大乘積子序列)

原題 Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. E

LeetCode 152. Maximum Product Subarray (最大乘積子陣列)

Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Exampl

leetcode | 152. Maximum Product Subarray

題目 Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Exa

[leetcode]152. Maximum Product Subarray

53題的升級版 https://mp.csdn.net/mdeditor/84856498# class Solution { public int maxProduct(int[] nums) { int[] premax=new int[nums.length

Crack LeetCode 之 152. Maximum Product Subarray

原題 下文除了程式碼部分外,整理自link 這道題跟Maximum Subarray類似,還是用一維動態規劃中的“區域性最優和全域性最優法”。區別在於累乘和累加的性質不一樣,累乘需要考慮負數的情況。如果前一個迭代的累乘結果為負數並且當前的元素也為負數的話,那麼當前元素與前

【LeetCode】152. Maximum Product Subarray 解題報告(Python)

題目描述: Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the l

LeetCode:152. Maximum Product Subarray(最大的乘積下標)

Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Examp

152. Maximum Product Subarray​​​​​​​

思路一:暴力。不同起點和終點的乘積挨個計算比較。 class Solution { public int maxProduct(int[] nums) { int max_pro=Integer.MIN_VALUE; int te

LeetCode---152. Maximum Product Subarray

題目 給出一個整數陣列,找到一個連續子序列,該子序列具有最大的乘積。 Python題解 class Solution(object): def maxProduct(self, num

算法(8)Maximum Product Subarray

開始 一個數 既然 bar 找到 部分 自己的 spa subarray 題目:在一個數組中找到一個子數組,讓子數組的乘積最大,比如【2,3,-2,4】返回6 思路:之前自己想到的思路是對於一個int類型的數組,只要負數的個數是偶數,那麽乘積肯定是全局乘就可以了,然後對於負

乘積最大子序列 Maximum Product Subarray

空間分析 連續 思路 特殊情況 time imu complex 英文 輸入 [抄題]: 找出一個序列中乘積最大的連續子序列(至少包含一個數)。 比如, 序列 [2,3,-2,4] 中乘積最大的子序列為 [2,3] ,其乘積為6。 [暴力解法]: 時間分析:每次j循環時都