https://leetcode.com/problems/maximum-product-subarray/
給定一個陣列,找到連續的最大乘積
利用for迴圈透過3個變數temp、cur、last紀錄暫時現在與上次的數值,在相互比較後回傳。
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;
}
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;
};
https://github.com/SIAOYUCHEN/leetcode
https://ithelp.ithome.com.tw/users/20100009/ironman/2500
https://ithelp.ithome.com.tw/users/20113393/ironman/2169
https://ithelp.ithome.com.tw/users/20107480/ironman/2435
https://ithelp.ithome.com.tw/users/20107195/ironman/2382
https://ithelp.ithome.com.tw/users/20119871/ironman/2210
https://ithelp.ithome.com.tw/users/20106426/ironman/2136
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.
如果暫時還找不到微笑的理由,那就放慢腳步享受這世界,因為微笑它可能正在來的路上。