LeetCode 812. 最大三角形面积
内容纲要

原题链接:https://leetcode.cn/problems/largest-triangle-area/

image-20240127230925067

枚举:

class Solution {
public:
    double S(int x1, int y1, int x2, int y2, int x3, int y3)//三角形面积公式(行列式)
    {
        return 0.5 * abs(x1 * y2 + x2 * y3 + x3 * y1 - x1 * y3 - x2 * y1 - x3 * y2);
    }

    double largestTriangleArea(vector<vector<int>>& points) {
        int n = points.size();
        double size = 0.0;
        for(int i = 0; i < n; i++)
        {
            for(int j = i + 1; j < n; j++)
            {
                for(int k = i + 2; k < n; k++)
                size = max(size, S(points[i][0], points[i][1], points[j][0], points[j][1], points[k][0], points[k][1]));
            }
        }
        return size;
    }
};

枚举2(记不到原公式,向量叉乘版):

class Solution {
public:
    double S(int x1, int y1, int x2, int y2, int x3, int y3)
    {
        double xx = x2 - x1;
        double yy = y2 - y1;
        double xxx = x3 - x1;
        double yyy = y3 - y1;
        return 0.5 * abs((xx * yyy - xxx * yy));//向量叉乘的几何意义是平行四边形的面积,除以二则是三角形的面积
    }

    double largestTriangleArea(vector<vector<int>>& points) {
        int n = points.size();
        double size = 0.0;
        for(auto p1 : points)
        {
            for(auto p2 : points)
            {
                for(auto p3 : points)
                size = max(size, S(p1[0], p1[1], p2[0], p2[1], p3[0], p3[1]));
            }
        }
        return size;
    }
};
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇