矩形

https://www.lintcode.com/problem/rectangle/description

/*
public class Point {
    public int x;
    public int y;
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
*/

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution {
    /**
     * @param pointSet: The point set
     * @return: The answer
     */
    public String rectangle(Point[] pointSet) {
        // Write your code here
        Map> xMap = new HashMap<>();
        Map> yMap = new HashMap<>();
        for (int i = 0; i < pointSet.length; i++) {
            Point point = pointSet[i];
            List points = xMap.get(point.x);
            if (points == null) {
                points = new ArrayList<>();
                xMap.put(point.x, points);
            }
            points.add(point);
            points = yMap.get(point.y);
            if (points == null) {
                points = new ArrayList<>();
                yMap.put(point.y, points);
            }
            points.add(point);
        }
        for (int i = 0; i < pointSet.length; i++) {
            Point point = pointSet[i];
//            从xMap中找一个
            List points = xMap.get(point.x);
            for (int j = 0; j < points.size(); j++) {
                Point point1 = points.get(j);
                if (point1 != point && point1.y != point.y) {
//                    从yMap中找一个
                    List points1 = yMap.get(point.y);
                    for (int k = 0; k < points1.size(); k++) {
                        Point point2 = points1.get(k);
                        if (point2 != point1 && point2 != point && point2.x != point.x) {
//                            三个点已经确定,找第四个点
//                            第四个点已经确定
                            int x = point2.x;
                            int y = point1.y;
                            for (int l = 0; l < pointSet.length; l++) {
                                Point point3 = pointSet[l];
                                if (point3.x == x && point3.y == y) {
                                    return "YES";
                                }
                            }
                        }
                    }
                }
            }
        }
        return "NO";
    }
}

你可能感兴趣的:(矩形)