Leetcode963. Minimum Area Rectangle II最小面积矩形2
给定在 xy 平面上的一组点,确定由这些点组成的任何矩形的最小面积,其中矩形的边不一定平行于 x 轴和 y 轴。
如果没有任何矩形,就返回 0。
示例 1:
输入:[[1,2],[2,1],[1,0],[0,1]] 输出:2.00000 解释:最小面积的矩形出现在 [1,2],[2,1],[1,0],[0,1] 处,面积为 2。
示例 2:
输入:[[0,1],[2,1],[1,1],[1,0],[2,0]] 输出:1.00000 解释:最小面积的矩形出现在 [1,0],[1,1],[2,1],[2,0] 处,面积为 1。
示例 3:
输入:[[0,3],[1,2],[3,1],[1,3],[2,1]] 输出:0 解释:没法从这些点中组成任何矩形。
示例 4:
输入:[[3,1],[1,1],[0,1],[2,1],[3,3],[3,2],[0,2],[2,3]] 输出:2.00000 解释:最小面积的矩形出现在 [2,1],[2,3],[3,3],[3,1] 处,面积为 2。
提示:
- 1 <= points.length <= 50
- 0 <= points[i][0] <= 40000
- 0 <= points[i][1] <= 40000
- 所有的点都是不同的。
- 与真实值误差不超过 10^-5 的答案将视为正确结果。
该题有很多需要注意的地方。
首先注意题目说的是矩形,而不是正方形。
然后 注意返回值是double型,所以我们要在题中使用double类型。
double类型是靠精度来比较的,而不是 ==
矩形的特征就是:对角线相等且平分
用其他方法要注意题中3个点在一条直线上的情况
这种多重循环的题,比如该题一般来说是n*n*n*n,我们可以优化成n*n*n。
const double esp = 1e-7;
class Solution {
public:
double minAreaFreeRect(vector<vector<int>>& points)
{
int len = points.size();
if (len < 4)
return 0;
double ans = -1;
map<pair<double, double>, bool> check;
for(int i = 0; i < len; i++)
{
check[make_pair(points[i][0], points[i][1])] = true;
}
for (int i = 0; i < len; i++)
{
double x1 = points[i][0], y1 = points[i][1];
for (int j = 0; j < len; j++)
{
if(i == j)
continue;
double x2 = points[j][0], y2 = points[j][1];
for (int k = 0; k < len; k++)
{
if(k == i || k == j)
continue;
double x3 = points[k][0], y3 = points[k][1];
double dis = GetDis(x1, y1, x2, y2);
//中点
double midx = (x1 + x2) * 1.0 / 2;
double midy = (y1 + y2) * 1.0 / 2;
if(abs(dis - GetDis(midx, midy, x3, y3) * 2) < esp)
{
double x4 = 2 * midx - x3;
double y4 = 2 * midy - y3;
if(check[make_pair(x4, y4)] == true)
{
double l = GetDis(x1, y1, x3, y3);
double w = GetDis(x1, y1, x4, y4);
double area = l * w;
ans = ans < 0? area : min(area, ans);
}
}
}
}
}
return ans < 0? 0 : ans;
}
//求距离
double GetDis(double x1, double y1, double x2, double y2)
{
return sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
}
};
Leetcode963. Minimum Area Rectangle II最小面积矩形2的更多相关文章
- [Swift]LeetCode963. 最小面积矩形 II | Minimum Area Rectangle II
Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 【LeetCode】963. Minimum Area Rectangle II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线段长+线段中心+字典 日期 题目地址:https: ...
- LC 963. Minimum Area Rectangle II
Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these ...
- 963. Minimum Area Rectangle II
Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these ...
- 计算几何-Minimum Area Rectangle II
2020-02-10 21:02:13 问题描述: 问题求解: 本题由于可以暴力求解,所以不是特别难,主要是用来熟悉计算几何的一些知识点的. public double minAreaFreeRect ...
- LeetCode939 最小面积矩形
LeetCode939最小面积矩形 给定在 xy 平面上的一组点,确定由这些点组成的矩形的最小面积,其中矩形的边平行于 x 轴和 y 轴. 如果没有任何矩形,就返回 0. Input [[1,1],[ ...
- [Swift]LeetCode939. 最小面积矩形 | Minimum Area Rectangle
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these p ...
- LeetCode 939. Minimum Area Rectangle (最小面积矩形)
题目标签:HashMap 题目给了我们一组 xy 上的点坐标,让我们找出 能组成矩形里最小面积的那个. 首先遍历所有的点,把x 坐标当作key 存入map, 把重复的y坐标 组成set,当作value ...
随机推荐
- python相关软件安装流程图解——虚拟机操作——复制虚拟机主机——CentOS-7-x86_64-DVD-1810
请先确保已经安装了虚拟机 python相关软件安装流程图解——虚拟机安装——CentOS-7-x86_64-DVD-1810——CentOS-01下载 https://www.cnblogs.com/ ...
- System.Drawing.Imaging.ImageFormat.cs
ylbtech-System.Drawing.Imaging.ImageFormat.cs 1.程序集 System.Drawing, Version=4.0.0.0, Culture=neutral ...
- System.Web.Mvc.HttpDeleteAttribute.cs
ylbtech-System.Web.Mvc.HttpDeleteAttribute.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral ...
- Java笔记 – JDBC编程
JDBC通过分层技术实现了跨数据库编程.为不同的数据库开发了统一的编程接口,为不同的数据库提供了不同的JAR类库. 一.JDBC基础 1.开发环境 (1)下载对应的Jar包 Oracle的本地Jar包 ...
- range()函数在python3与python2中的区别
range()函数在python3与python2中的区别 - CSDN博客 https://blog.csdn.net/weixin_37579123/article/details/8098038 ...
- IDEA 创建普通的maven+java Project
最近想把以前积累的零散java练习和学习的东西建一个项目整理出来上传到码云托管,免得电脑挂了啥也找不到 配置是IDEA2017+java8+maven3.2.5,截图记录下步骤 第一步:File--& ...
- ssm 框架整合 代码初步 maven配置
pom.xml 配置<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <de ...
- Go前言
Go语言为并发而生 硬件制造商正在为处理器添加越来越多的内核以来提高性能.所有数据中心都在这些处理器上运行,今天的应用程序使用多个微服务来维护数据库连接,消息队列和维护缓存.所以,开发的软件和编程语言 ...
- easyui treegrid的使用示例
一.前端: <div id="tbList" fit="true"></div> $(function () { $("#tb ...
- 【数位DP】[LOJ10168] 恨7不成妻
还是数位DP... 状态:$f[x][val][sum]$表示当前第x位,当前数字为val,当前各位数字和为sum 观察到$val$,$sum$过大,很套路地模7即可... 每个状态存储三个要用到的值 ...