Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.

If there isn't any rectangle, return 0.

Example 1:

Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2: Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2 Note: 1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct.

核心思想是利用对角线的原理定下两个点,根据对角线在找到另外两个点

如何把一个array pair存到hash里,第一次我用了int -> string, 超时了

class Solution {
public int minAreaRect(int[][] points) {
if(points == null || points.length == 0 || points[0].length ==0){
return -1;
}
Set<String> set = new HashSet<>();
for(int[] point : points){
set.add(Integer.toString(point[0])+":"+Integer.toString(point[1]));
}
int min = Integer.MAX_VALUE;
for(int i = 0; i < points.length-1; i++){
for(int j = i+1; j < points.length; j++){
int [] pointA = points[i];
int [] pointB = points[j];
if(pointA[0] != pointB[0] && pointA[1] != pointB[1]){
if(set.contains(Integer.toString(pointA[0])+":"+Integer.toString(pointB[1])) && set.contains(Integer.toString(pointB[0])+":"+Integer.toString(pointA[1]))){
int cur = Math.abs((pointB[0] - pointA[0]) * (pointB[1] - pointA[1]));
if(cur < min){
min = cur;
}
}
}
}
}
if(min == Integer.MAX_VALUE){
return 0;
}
return min;
}
}

第二次看了别人的代码, 原来可以按照把每一个pair的x存成key,y轴存成一个hashset。

class Solution {
public int minAreaRect(int[][] points) {
if(points == null || points.length == 0 || points[0].length ==0){
return 0;
}
Map<Integer, Set<Integer>> map = new HashMap<>();
for(int[] point : points){
if(map.containsKey(point[0])){
map.get(point[0]).add(point[1]);
}
else{
Set<Integer> set = new HashSet<Integer>();
set.add(point[1]);
map.put(point[0], set);
}
} int min = Integer.MAX_VALUE;
for(int i = 0; i < points.length-1; i++){
for(int j = i+1; j < points.length; j++){
int [] pointA = points[i];
int [] pointB = points[j];
if(pointA[0] != pointB[0] && pointA[1] != pointB[1]){
if(map.get(pointA[0]).contains(pointB[1]) && map.get(pointB[0]).contains(pointA[1])){
int cur = Math.abs((pointB[0] - pointA[0]) * (pointB[1] - pointA[1]));
if(cur < min){
min = cur;
}
}
}
}
}
if(min == Integer.MAX_VALUE){
return 0;
}
return min;
}
}

LeetCode - Minimum Area Rectangle的更多相关文章

  1. 【leetcode】963. Minimum Area Rectangle II

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

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

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

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

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

  6. 【LeetCode】939. Minimum Area Rectangle 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 确定对角线,找另外两点(4sum) 字典保存出现的x ...

  7. 【leetcode】939. Minimum Area Rectangle

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

  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. java字符串根据空格截取并存进list,并在每个元素前后加上/

    public class List1 { public static void main(String[] args) { String s = "abc nnn ooo/xzsxc bs& ...

  2. python学习 面向对象高级编程

    ---恢复内容开始--- 面向对象编程---oop,是一种编程思想,oop把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程的程序设计把计算机程序视为一系列的命令集合,即一组函数 ...

  3. (C/C++学习笔记) 五. 常变量(只读变量)和宏

    五. 常变量(只读变量)和宏 ● 常变量 常变量 #include <iostream.h>                    //预处理文件 int main() { const d ...

  4. 关于A中用到B, B中用到A的问题

    //A.h #ifndef AH #define AH class B; class A { public: B* b; void setB(); ~A(); }; #endif //B.h #ifn ...

  5. 1.1最简单的socket连接

    socket 服务器代码 # -*- coding: utf-8 -*-from socket import * myHost = '' #''说明所有IP都可以连接 myPort = 50007 # ...

  6. vivado对task和function的可综合支持

    手册UG901,对vivado可综合的语句支持进行了描述,HDL包括:verilog-2001,system-verilog,VHDL: verilog-2001扩展了对task和function的支 ...

  7. JSONP解决跨域问题,什么是JSONP(转)

    原文链接:https://www.cnblogs.com/xinxingyu/p/6075881.html 说到AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交换数据?第二个是跨域的 ...

  8. 201621123001 《Java程序设计》第8周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 1. ArrayList代码分析 1.1 解释ArrayList的contains源代码 Answer: 源 ...

  9. url中特殊符号转换

    特殊符号 url中显示 空格 %20 & %26

  10. 20165214 2017-2018-2 《Java程序设计》课程总结

    20165214 2017-2018-2 <Java程序设计>课程总结 每周任务链接 预备作业1:我期望的师生关系 预备作业2:C语言基础调查和java学习展望 预备作业3:Linux安装 ...