题目一:![](https://i-blog.csdnimg.cn/blog_migrate/56326988ad06bc2ff38c0e7e9fe8ef8e.png)
int findMaxConsecutiveOnes(int* nums, int numsSize)
{
int cnt1=0;
int cnt2=0;
for(int i=0;i<numsSize;i++)
{
if(nums[i]==1)
cnt1++;
if(cnt1>cnt2)
{
cnt2=cnt1;
}
if(nums[i]!=1)
cnt1=0;
}
return cnt2;
}
题目2:![](https://i-blog.csdnimg.cn/blog_migrate/e2208cf8b119b5a97f085a5ce8bbf045.png)
方法一:排序
int change(void*num1,void*num2)
{
return *(int*)num1-*(int*)num2;
}
int maxProduct(int* nums, int numsSize)
{
qsort(nums,numsSize,sizeof(int),change);//change代表了元素的间隔
return (nums[numsSize-2]-1)*(nums[numsSize-1]-1);
}
方法二:线性枚举
int maxProduct(int* nums, int numsSize)
{
int max=nums[0];
int smax=nums[1];
for(int i=1;i<numsSize;i++)
{
if(nums[i]>max)
{
smax=max;
max=nums[i];
}
else if(nums[i]==max)
{
smax=nums[i];
}
else if(nums[i]>smax&&nums[i]<max)
smax=nums[i];
}
int ret=(max-1)*(smax-1);
return ret;
}
题目3:![](https://i-blog.csdnimg.cn/blog_migrate/f5f97d3868e427a5dbdd7cbace483d35.png)
int change(void*num1,void*num2)
{
return *(int*)num1-*(int*)num2;
}
int findMin(int* nums, int numsSize)
{
qsort(nums,numsSize,sizeof(int),change);
return nums[0];
}
题目4:![](https://i-blog.csdnimg.cn/blog_migrate/c9c8747e15ccf1ff6f6285ef4f11a3b8.png)
//二分查找
int findMin(int* nums, int numsSize)
{
int right=numsSize-1;
int left=0;
while(left<right)
{
int mid=(right+left)/2;
if(nums[mid]>nums[right])
{
left=mid+1;
}
else if(nums[mid]<nums[right])
right=mid;
else if(nums[mid]==nums[right])
right-=1;
}
return nums[left];
}
题目5:![](https://i-blog.csdnimg.cn/blog_migrate/38b651bb7839d43e7454065a713bae2f.png)
int thirdMax(int* nums, int numsSize)
{
long long first=LONG_MIN;
long long second=LONG_MIN;
long long third=LONG_MIN;
if(numsSize==2)
{
return nums[0]>nums[1]?nums[0]:nums[1];
}
else if(numsSize==1)
return nums[0];
else if(numsSize>2)
{
for(int i=0;i<numsSize;i++)
{
if(nums[i]>first)
{
third=second;
second=first;
first=nums[i];
}
else if(nums[i]<first&&nums[i]>second)
{
third=second;
second=nums[i];
}
else if(nums[i]<second&&nums[i]>third)
{
third=nums[i];
}
}
}
if(third==LONG_MIN)
return first;
return third;
}
题目6:![](https://i-blog.csdnimg.cn/blog_migrate/b8f5d01d959990dd41c3a4ab66245b4d.png)
int maximumProduct(int* nums, int numsSize)
{
int sfirst=INT_MAX;
int ssecond=INT_MAX;
int first=INT_MIN;
int second=INT_MIN;
int third=INT_MIN;
for(int i=0;i<numsSize;i++)
{
if(nums[i]<=sfirst)
{
ssecond=sfirst;
sfirst=nums[i];
}
else if(nums[i]>sfirst&&nums[i]<=ssecond)
ssecond=nums[i];
if(nums[i]>=first)
{
third=second;
second=first;
first=nums[i];
}
else if(nums[i]<first&&nums[i]>=second)
{
third=second;
second=nums[i];
}
else if(nums[i]<second&&nums[i]>=third)
{
third=nums[i];
}
}
return first*second*third>first*ssecond*sfirst?first*second*third:first*sfirst*ssecond;
}