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 ...
随机推荐
- Android系统开发 编译系统签名的APP
前言 一般情况下,我们使用的签名都是自己生成的Java签名来编译APP. 但是,如果需要开发一些特定设备的APP(对权限有更高的要求,需求一些系统基本的权限,比如让APP可以控制设备的休眠),那就需要 ...
- SQL 标量函数-----日期函数 day() 、month()、year() 转载
select day(createtime) from life_unite_product --取时间字段的天值 select month(createtime) from life_u ...
- js基础(条件语句 循环语句)
条件语句 if语句块的语法形式如下: //只有两种情况下if(条件){要执行的语句块;}else{要执行的语句块;} //多种情况下if(条件){要执行的语句块;}else if(条件){要执行的语句 ...
- thinkphp Trace方法
页面Trace只能用于有页面输出的情况,但是trace方法可以用在任何情况,而且trace方法可以用于AJAX等操作. Trace方法的格式: trace('变量','标签','级别','是否记录日志 ...
- 欧拉定理+质因子分解+矩阵快速幂——cf1182E
好题! /* gi=c^i * fi gi=gi-1 * gi-2 * gi-3 把g1,g2,g3质因数分解 g1=p1^e11 * p2^e12 * p3^e13 ... pk^e1k g2=p1 ...
- 图片压缩(js压缩,底部有vue压缩图片依赖使用的教程链接)
directTurnIntoBase64(fileObj, callback) { var r = new FileReader(); // 转成base64 r.onload = function( ...
- fatal error U1087: cannot have : and :: dependents for same target Stop.
转自VC错误:http://www.vcerror.com/?p=72 问题描述: 完成后编译,发现有错误 D:\WinDDK\7600.16385.1\bin\makefile.new(7117) ...
- iOS开发之SceneKit框架--实战地月系统围绕太阳旋转
1.创建地月太阳系统scn文件 注意:moon在earth结构下,earth和moon在sun结构下. 2.获取scn中模型的对应节点和自转(太阳为例) 获取节点: name是对应的Identity字 ...
- Assert(断言) 的用法
Assert Assert是断言的意思,头文件为assert.h, assert是一个宏 功 能: 测试一个条件并可能使程序终止 用 法: void assert(int test); 在单元测试中经 ...
- node-webkit笔记
两个月前给一个运营站点做了个封皮,今天再做竟然忘了怎么搞了...为之文以志. 流程参考: http://www.cnblogs.com/2050/p/3543011.html 相关命令: copy / ...