三角形的最大周长 - 简单

*************

C++

topic: 976. 三角形的最大周长 - 力扣(LeetCode)

*************

Give the topic a inspection.

I tend to immediately think of sort algorithm. The longest perimeter of a triangle.  If you went  to primary school once, you will know a low that the sumations of the two shorter lines are biger than the longer one.

Today I learn a new trick about sort. Actually I learned how to use sort aigorithm before. But today I will learn something new. Sort algorithm is used to order the vector.

the basic form 

sort(容器.begin(), 容器.end());

default increasing order

vector<int> vec = {5, 1, 3, 2, 4};

sort(vec.begin(), vec.end());
// 输出: 1 2 3 4 5

if you want decreasing order

vector<int> vec = {5, 2, 9, 1, 5, 6};

// 使用 greater 进行降序排序
sort(vec.begin(), vec.end(), greater<int>());

// 输出:9 6 5 5 2 1

beside this, you should learn another way. use compare function.

// 自定义比较函数(降序)
bool compare(int a, int b) 
{
    return a > b; // 当 a > b 时,a 排在前面
}


vector<int> vec = {5, 1, 3, 2, 4};
sort(vec.begin(), vec.end(), compare); // 传入函数名
// 输出: :5 4 3 2 1

and use lambda function.

vector<int> vec = {5, 1, 3, 2, 4};

sort(vec.begin(), vec.end(), [](int a, int b) { return a > b; });
// 输出: 5 4 3 2 1

So I decide to sort the vector first.

class Solution {
public:
    int largestPerimeter(vector<int>& nums) {

        // 对nums进行从大到小排列
        bool compare(int a, int b)
        {
            return a > b;
        }

        sort(nums.begin(), nums.end(), compare);
    }
};

Then calculate the sumation of the longest three lines.

class Solution {
public:
    int largestPerimeter(vector<int>& nums) {

        // 对nums进行从大到小排列
        bool compare(int a, int b)
        {
            return a > b;
        }

        sort(nums.begin(), nums.end(), compare);

        int perimeter = 0; // 用来储存三角形周长

        for (int i = 0; i < 3; i++)
        {
            perimeter += nums[i];
        }

        return perimeter;
    }
};

Here in the function A, you cannot define another function B. So move the comapre function out of the largestPerimeter function. Then call the canpare function.

class Solution {
public:
    // 对nums进行从大到小排列
    static bool compare(int a, int b) 
    { 
        return a > b; 
    }

    int largestPerimeter(vector<int>& nums) 
    {

        // 对nums进行从大到小排列
        sort(nums.begin(), nums.end(), compare);

        int perimeter = 0; // 用来储存三角形周长

        for (int i = 0; i < 3; i++) 
        {
            perimeter += nums[i];
        }

        return perimeter;
    }
};

However, something goes wrong.

If you went  to primary school once, you will know a low that the sumations of the two shorter lines should be stronger than the longer one.

class Solution {
public:
    // 对nums进行从大到小排列
    static bool compare(int a, int b) 
    { 
        return a > b; 
    }

    int largestPerimeter(vector<int>& nums) 
    {
        // 对nums进行从大到小排列
        sort(nums.begin(), nums.end(), compare);

        // 遍历数组,尝试每三个连续的元素
        for (int i = 0; i < nums.size() - 2; i++) 
        {
            // 检查是否满足三角形条件
            if (nums[i] < nums[i+1] + nums[i+2]) 
            {
                // 如果满足条件,返回这三个数的和
                return nums[i] + nums[i+1] + nums[i+2];
            }
        }

        // 如果没有找到满足条件的三个数,返回0
        return 0;
    }
};

You can make the code more concise.

class Solution {
public:
    int largestPerimeter(vector<int>& nums) {

        // 对nums进行default order
        sort(nums.begin(), nums.end());

        // 既然是从小到大排列,从后往前iterate
        int n = nums.size();
        for (int i = n - 1; i >= 2; i--)
        {
            if (nums[i] < nums[i - 1] + nums[i - 2])
            {
                return nums[i] + nums[i - 1] + nums[i - 2];
            }
        }
        return 0;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值