DAY 12
0

## 題目:

https://leetcode.com/problems/maximum-product-subarray/

## 解題思路:

### C版本:

``````int maxProduct(int* nums, int numsSize) {
int max=nums[0];
int lastmax=nums[0];
int lastmin=nums[0];
int curmax,curmin;
int temp1;
int temp2;
if(numsSize==1) return nums[0];
for(int i=1;i<numsSize;i++)
{
temp1=lastmax*nums[i];
temp2=lastmin*nums[i];
curmax=temp1>temp2?temp1:temp2;
curmax=curmax>nums[i]?curmax:nums[i];
curmin=temp1>temp2?temp2:temp1;
curmin=curmin>nums[i]?nums[i]:curmin;
max=max>curmax?max:curmax;
lastmax=curmax;
lastmin=curmin;
}
return max;
}
``````

### Javascript版本:

``````var maxProduct = function(nums) {
var max=nums[0];
var lastmax=nums[0];
var lastmin=nums[0];
var curmax,curmin;
var temp1;
var temp2;
if(nums.length==1) return nums[0];
for(var i=1;i<nums.length;i++)
{
temp1=lastmax*nums[i];
temp2=lastmin*nums[i];
curmax=temp1>temp2?temp1:temp2;
curmax=curmax>nums[i]?curmax:nums[i];
curmin=temp1>temp2?temp2:temp1;
curmin=curmin>nums[i]?nums[i]:curmin;
max=max>curmax?max:curmax;
lastmax=curmax;
lastmin=curmin;
}
return max;
};
``````

## 程式Github分享:

https://github.com/SIAOYUCHEN/leetcode

## 本日分享:

If you can’t find a reason for a while, the let slow down and enjoy the world by yourself, because smiling may be on the way.