《算法零基础》第17讲:线性枚举(1)

485. 最大连续 1 的个数

原题链接:最大连续 1 的个数
在这里插入图片描述

分析

简单的遍历方法,定义一个计数器,遇到1就++,遇到零置为0。
循环每走一步就和上一次的计数器存的值比较,返回大的数。

代码

class Solution {
public:

    int Max(int a, int b)
    {
        return a > b ? a : b;
    }
    int findMaxConsecutiveOnes(vector<int>& nums)
    {
        int count = 0, ans = 0;
        for (int i = 0; i < nums.size(); ++i)
        {
            if (nums[i] == 1)
            {
                count++;
                ans = Max(ans, count);
            }
            else
            {
                count = 0;
            }
        }
        return ans;
    }
};

LeetCode 1464. 数组中两元素的最大乘积

原题链接:数组中两元素的最大乘积

在这里插入图片描述

分析

根据题意,就是让找出第一大和第二大的元素。并且范围内没有负数,不用考虑负数了。

方法:
1.直接遍历。
2.排序后取数组最后两个元素(不推荐)

代码

int maxProduct(int* nums, int numsSize)
{
    int max = nums[0];
    int nextmax = nums[1];

    for (int i = 1; i < numsSize; ++i)
    {
        if (nums[i] > max)
        {
            nextmax = max;
            max = nums[i];
        }
        else if (nums[i] == max)
        {
            nextmax = nums[i];
        }
        else if (nums[i] > nextmax && nums[i] < max)
        {
            nextmax = nums[i];
        }
    }
 
    return (max - 1) * (nextmax - 1);
}

153. 寻找旋转排序数组中的最小值

原题链接: 寻找旋转排序数组中的最小值

在这里插入图片描述

代码

class Solution {
public:
    int findMin(vector<int>& nums)
    {
        int min = INT_MAX;
        for (int i = 0; i < nums.size(); ++i)
        {
            if (min > nums[i])
            {
                min = nums[i];
            }
        }
        return min;
    }
};

414. 第三大的数

原题链接:414. 第三大的数

在这里插入图片描述

代码1:遍历

int thirdMax(int* nums, int numsSize)
{
    int max, second, third;
    int min = nums[0];
    for (int i = 0; i < numsSize; ++i)
    {
        if (min >= nums[i])
        {
            min = nums[i];
        }
    }
    max = second = third = min;

    for (int i = 0; i < numsSize; ++i)
    {
        if (nums[i] > max)
        {
            third = second;
            second = max;
            max = nums[i];
        }
        else if (nums[i] > second && nums[i] != max)
        {
            third = second;
            second = nums[i];
        }
        else if (nums[i] > third && nums[i] != second && nums[i] != max)
        {
            third = nums[i];
        }
    }
    if (max == second || second == third)
    {
        return max;
    }
    return third;
}

628. 三个数的最大乘积

原题链接:628. 三个数的最大乘积

在这里插入图片描述

分析

找出数组中最大的三个数,和两个最小的负数。

用三个最大数的乘积和两个最小负数与最大值的乘积比较

返回大的。

代码

int mycmp(const void* p1, const void* p2)
{
    const int* a = (const int*)p1;
    const int* b = (const int*)p2;
    return *a - *b;
}

int maximumProduct(int* nums, int numsSize)
{
    if (NULL == nums) return 0;
    int n = numsSize;
    qsort(nums, n, sizeof(int), mycmp);
    
    return fmax(nums[0]*nums[1]*nums[n - 1], nums[n - 1]*nums[n - 2]*nums[n - 3]);
}

C++代码

class Solution {
public:
    int maximumProduct(vector<int>& nums)
    {
        sort(nums.begin(), nums.end());
        int n = nums.size();

        return max(nums[0]*nums[1]*nums[n - 1] , nums[n - 1]*nums[n - 2]*nums[n - 3]);
    }
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_索伦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值