给定在 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. Python 输入字符串找(String)下标 没有返回-1

    str = "abcdefg123456"a = input("请输入一个字母或数字:")num = 0result = -1while num < le ...

  2. leetcode-227-基本计算器②

    题目描述: 方法一:中缀转后缀 #!_*_coding:utf-8_*_ class Solution: def calculate(self, s: str) -> int: def in_t ...

  3. TopCoder[SRM513 DIV 1]:PerfectMemory(500)

    Problem Statement      You might have played the game called Memoria. In this game, there is a board ...

  4. linux如何查看防火墙是否开启?删除iptables规则

    iptables是linux下的防火墙组件服务,相对于windows防火墙而言拥有更加强大的功能,此经验咗嚛以centos系统为例.关于iptables的一般常见操作,怎么来判断linux系统是否启用 ...

  5. PAT甲级——A1104 Sum of Number Segments【20】

    Consider a positive integer N written in standard notation with k+1 digits a​i​​ as a​k​​⋯a​1​​a​0​​ ...

  6. 通过java进行电脑屏幕截图

    package image; import java.awt.Desktop; import java.awt.Dimension; import java.awt.Rectangle; import ...

  7. 使用CEfSharp之旅(6)拦截网络请求 截取response返回

    原文:使用CEfSharp之旅(6)拦截网络请求 截取response返回 版权声明:本文为博主原创文章,未经博主允许不得转载.可点击关注博主 ,不明白的进群191065815 我的群里问 https ...

  8. 9个永恒的UI设计原则

    很多人都在寻找那些能够帮助他们快速提升设计能力的方法,但你是否想过,自己身上的哪些方面会对你的设计产生影响呢?是使用工具的技巧,对设计的理解和态度,还是你的生活习惯呢?我想说所有这些都是决定你的设计是 ...

  9. JS流程控制语句 多重判断满足你各种需求 要在多组语句中选择一组来执行,使用if..else嵌套语句。

    多重判断(if..else嵌套语句) 要在多组语句中选择一组来执行,使用if..else嵌套语句. 语法: if(条件1) { 条件1成立时执行的代码} else if(条件2) { 条件2成立时执行 ...

  10. Ascii码 、16进制与 char

            对于一个非计算机专业出身的人,以前只知道计算机中所有的数据都是以二进制形式进行存储,计算,通信的.但是人类文明中,主要的信息展现以文本的形式展现的.如果使用内存中的0和1来表示文本一直 ...