[LeetCode] Largest Triangle Area 最大的三角区域
You have a list of points in the plane. Return the area of the largest triangle that can be formed by any 3 of the points.
Example:
Input: points = [[0,0],[0,1],[1,0],[0,2],[2,0]]
Output: 2
Explanation:
The five points are show in the figure below. The red triangle is the largest.

Notes:
3 <= points.length <= 50.- No points will be duplicated.
-50 <= points[i][j] <= 50.- Answers within
10^-6of the true value will be accepted as correct.
这道题给了我们一系列的二维平面上的点,让我们找出任意三个点能组成的最大三角形的面积。那么我们只能遍历所有的三角形面积,然后找出最大的那个。貌似这道题也没有啥特别简便的方法,不遍历不行啊。遍历任意三个点简单,问题来了,如何通过三个顶点的坐标求出三角形面积,这个可就是初中几何题了,博主也不记得,只能上网搜一波。就是用下面这个公式即可:

这里面三个顶点分别是(x1, y1),(x2, y2),(x3, y3),有了公式后,本题就没有什么难点了,参见代码如下:
解法一:
class Solution {
public:
double largestTriangleArea(vector<vector<int>>& points) {
double res = ;
for (int i = ; i < points.size(); ++i) {
for (int j = i + ; j < points.size(); ++j) {
for (int k = j + ; k < points.size(); ++k) {
int x1 = points[i][], y1 = points[i][];
int x2 = points[j][], y2 = points[j][];
int x3 = points[k][], y3 = points[k][];
double area = abs(0.5 * (x2 * y3 + x1 * y2 + x3 * y1 - x3 * y2 - x2 * y1 - x1 * y3));
res = max(res, area);
}
}
}
return res;
}
};
我们也可以稍稍简化一下上面的写法,但是解题思路没有任何区别,参见代码如下:
解法二:
class Solution {
public:
double largestTriangleArea(vector<vector<int>>& points) {
double res = ;
for (auto &i : points) {
for (auto &j : points) {
for (auto &k : points) {
res = max(res, 0.5 * abs(i[] * j[] + j[] * k[] + k[] * i[]- j[] * i[] - k[] * j[] - i[] * k[]));
}
}
}
return res;
}
};
参考资料:
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Largest Triangle Area 最大的三角区域的更多相关文章
- 【Leetcode_easy】812. Largest Triangle Area
problem 812. Largest Triangle Area solution: class Solution { public: double largestTriangleArea(vec ...
- 【LeetCode】812. Largest Triangle Area 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 三重循环 组合函数 日期 题目地址:https:// ...
- LeetCode 812 Largest Triangle Area 解题报告
题目要求 You have a list of points in the plane. Return the area of the largest triangle that can be for ...
- [Swift]LeetCode812. 最大三角形面积 | Largest Triangle Area
You have a list of points in the plane. Return the area of the largest triangle that can be formed b ...
- 812. Largest Triangle Area
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- Leetcode812.Largest Triangle Area最大三角形面积
给定包含多个点的集合,从其中取三个点组成三角形,返回能组成的最大三角形的面积. 示例: 输入: points = [[0,0],[0,1],[1,0],[0,2],[2,0]] 输出: 2 解释: 这 ...
- Leetcode#118. Pascal's Triangle(杨辉三角)
题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...
- Largest Rectangular Area in a Histogram
题目地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ ,刚開始事实上没做这个题,而是在做https://oj.le ...
- leetcode-812-Largest Triangle Area
题目描述: You have a list of points in the plane. Return the area of the largest triangle that can be fo ...
随机推荐
- java.lang.OutOfMemoryError: Java heap space内存不足问题
今晚,在定义一个new int[19001][13001]的数组时候内存不够:特转了一下方法: Exception in thread "main" java.lang.OutOf ...
- SpringBoot系列: 设计Restful风格的API
RESTful 架构REST 并非一种技术或规范, 而是一种架构风格, 如果一个架构符合Rest的约束条件和原则, 就可以称作是 RESTful 架构. REST全称是Representational ...
- Chrome firefox ie等浏览器空格 宽度不一样
用半角空格 或者全角空格 相当于半格中文字符的宽度, 相当于一个中文字符宽度. 注:在chrome中两个 占一个汉字的宽度;,而在IE.firefox中四个 才占一个汉字的宽度.
- Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)
题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...
- C# - 设计模式 - 策略模式
策略模式 问题场景 多个类型都有一些共同的属性和方法,可以称这些成员为行为,为了避免重复在多个类型中编码相同部分的行为,应考虑将这些行为定义在抽象类(超类)中,利用继承时多个类型可以共享这些行为.比如 ...
- Stm32型号查阅手册
- Spring系列(一) Spring的核心
Spring 简介 Spring 是一个开源轻量级企业应用架构,目的是为了简化企业级应用开发.(1)Spring 框架可以帮我们管理对象的生命周期,帮助我们管理对象间的依赖关系,相互协作:(2)Spr ...
- Java代码操作HDFS
package com.hy.hdfs; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; imp ...
- Ubuntu查看端口占用情况
netstat -apn 其中最后一列是PID,可以通过kill Pid进行结束进程. 更精确的查找: netstat -apn | grep 8080 查询8080端口的进程 如果要查询这个进程的详 ...
- OpenCV3编程入门读书笔记5-边缘检测
一.边缘检测的一般步骤 1.滤波 边缘检测的算法主要是基于图像强度的一阶和二阶导数,但导数通常对噪声很敏感,因此必须采用滤波器来改善与噪声有关的边缘检测器的性能. 2.增强 增强边缘的基础是确定图像各 ...