给定在 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. Yii2 中使用ts

    在运行环境 vagrant Ubuntu box 中安装 sass ,typescript等 安装需要的软件: sudo su -c "gem install sass" # 可选 ...

  2. 廖雪峰Java16函数式编程-2Stream-5filter

    1.filter简介 Stream.filter()是一个转换方法,把一个Stream转换为另一个Stream. 所谓filter操作,就是对一个Stream的所有元素进行测试,不满足条件的元素就被过 ...

  3. win10 mysql5.7指定某个配置文件启动

    点击开始菜单,搜索cmd.exe,左击以管理员身份运行 C:\Users\Administrator>cd C:\Program Files\MySQL\MySQL Server 5.7\bin ...

  4. VS2010-MFC(常用控件:列表视图控件List Control 上)

    转自:http://www.jizhuomi.com/software/195.html      列表视图控件简介 列表视图控件List Control同样比较常见,它能够把任何字符串内容以列表的方 ...

  5. Facebook分布式框架—Thrift介绍。

    Thrift介绍 Thrift是一个分布式RPC框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, ...

  6. vue使用axios实现前后端通信

    安装依赖 npm install --save axios # vue-axios是对axios的简单封装 npm install --save vue-axios 用例 在main.js里面进行全局 ...

  7. Neo4j全文检索

    全文检索基本概念 搜索 搜索这个行为是用户与搜索引擎的一次交互过程,用户需要找一些数据,他提供给搜索引擎一些约束条件.搜索引擎通过约束条件抽取一些结果给用户 搜索引擎 搜索引擎存在的目的是存储,查找和 ...

  8. Spark如何读写hive

    原文引自:http://blog.csdn.net/zongzhiyuan/article/details/78076842 hive数据表建立可以在hive上建立,或者使用hiveContext.s ...

  9. PHP网络请求优化

    目录 1. 设置超时时间 2. 将串行请求并行化 1. 设置超时时间 连接超时:200ms 读超时: 800ms 写超时: 500ms 2. 将串行请求并行化 使用curl_multi_*() 使用s ...

  10. Linux-c 线程锁

      typedef struct _my_mutex { pthread_mutex_t mutex; //互斥锁 pthread_mutexattr_t mta; //互斥锁属性 } my_mute ...