给定在 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. 1 <= points.length <= 50
  2. 0 <= points[i][0] <= 40000
  3. 0 <= points[i][1] <= 40000
  4. 所有的点都是不同的。
  5. 与真实值误差不超过 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的更多相关文章

  1. [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 ...

  2. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  3. 【LeetCode】963. Minimum Area Rectangle II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线段长+线段中心+字典 日期 题目地址:https: ...

  4. 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 ...

  5. 963. Minimum Area Rectangle II

    Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these ...

  6. 计算几何-Minimum Area Rectangle II

    2020-02-10 21:02:13 问题描述: 问题求解: 本题由于可以暴力求解,所以不是特别难,主要是用来熟悉计算几何的一些知识点的. public double minAreaFreeRect ...

  7. LeetCode939 最小面积矩形

    LeetCode939最小面积矩形 给定在 xy 平面上的一组点,确定由这些点组成的矩形的最小面积,其中矩形的边平行于 x 轴和 y 轴. 如果没有任何矩形,就返回 0. Input [[1,1],[ ...

  8. [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 ...

  9. LeetCode 939. Minimum Area Rectangle (最小面积矩形)

    题目标签:HashMap 题目给了我们一组 xy 上的点坐标,让我们找出 能组成矩形里最小面积的那个. 首先遍历所有的点,把x 坐标当作key 存入map, 把重复的y坐标 组成set,当作value ...

随机推荐

  1. [转]springmvc+mybatis需要的jar包与详解

    1.antlr-2.7.6.jar:  项目中没有添加,hibernate不会执行hql语句 2.Aopalliance.jar: 这个包是AOP联盟的API包,里面包含了针对面向切面的接口,通常Sp ...

  2. APB简介

    一.血缘 AMBA: Advanced Microcontroller Bus Architecture 高级处理器总线架构 AHB: Advanced High-performance Bus 高级 ...

  3. [JZOJ1904] 【2010集训队出题】拯救Protoss的故乡

    题目 题目大意 给你一个树形的网络,每条边从父亲流向儿子.根节点为原点,叶子节点流向汇点,容量为无穷大. 可以给一些边扩大容量,最多总共扩大\(m\)容量.每条边的容量有上限. 求扩大容量后最大的最大 ...

  4. RPC远程过程调用实例详解

    1.创建IDL文件,定义接口. IDL文件可以由uuidgen.exe创建. 首先找到系统中uuidgen.exe的位置,如:C:\Program Files\Microsoft Visual Stu ...

  5. python笔记四

    #!/usr/bin/env python3 from datetime import datetime, timedelta # datetime是模块,datetime模块还包含一个datetim ...

  6. R软件导入数据_r语言怎么导入数据_R软件导入数据

    R软件导入数据_r语言怎么导入数据_R软件导入数据 R软件导入数据 1.Rcmdr安装包导入数据: 1.安装Rcmdr包,输入: install.packages("Rcmdr") ...

  7. k8s常用的资源

    1. 创建pod资源 pod是最小的资源单位 任何一个k8s资源都可以有yml清单文件来定义 k8s yaml的主要组成 apiVersion: v1 api版本 kind: pod 资源类型 met ...

  8. java-day04

    IntelliJ快捷键 导入包 alt + enter 删除光标所在行 ctrl + y 复制光标所在行 ctrl + d 格式代码 ctrl + alt + l 单行注释 ctrl + / 多行注释 ...

  9. Python第二课-输入输出

    name = input() 输入的字符串已经赋值给变量name print() 输出内容 print(,) print中,连接字符串相当于空格

  10. AngularJs 报错 Error: [$parse:lexerr]

    参考:https://www.cnblogs.com/fangshidaima/p/6048071.html 错误: 根据错误找到报错行: $scope.$apply($scope.param1 = ...