最大子序和
float fmaxf( float x, float y );
double fmax( double x, double y );
long double fmaxl( long double x, long double y );
int maxSubArray(int* nums, int numsSize) {
int pre = 0, maxAns = nums[0];
for (int i = 0; i < numsSize; ++i) {
pre = fmax(pre + nums[i], nums[i]);//fmax is max double
maxAns = fmax(maxAns, pre);
}
return maxAns;
}
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int pre = 0;
int Maxs = nums[0];
for (const auto& x : nums)
{
pre = max(pre+x,x);
Maxs = max(pre,Maxs);
}
return Maxs;
}
};
int maxSubArray(int* nums, int numsSize){
int result = INT_MIN, f = 0;
for (int i = 0; i < numsSize; ++i) {
f = fmax(f + nums[i], nums[i]);
result = fmax(result, f);
}
return result;
}