Leetcode 939. 最小面积矩形 (枚举对角线两个点,点的hash,然后判断矩形面积)

Leetcode 939. 最小面积矩形 (枚举对角线两个点,点的hash,然后判断矩形面积)

class Solution {
public:
    int minAreaRect(vector<vector<int>>& points) {
        set<pair<int, int>> pointSet;
        for(auto point : points) {
            pointSet.insert({point[0], point[1]});
        }
        int res = INT_MAX;
        for (int i = 0; i < points.size(); i++) {
            int x0 = points[i][0];
            int y0 = points[i][1];
            for (int j = i + 1; j < points.size(); j++) {
                int x1 = points[j][0];
                int y1 = points[j][1];
                pair<int, int> p1 {x0, y1};
                pair<int, int> p2 {x1, y0};
                if (x0 != x1 && y0 != y1 && pointSet.count(p1) && pointSet.count(p2)) {
                    res = min(res, abs((x0 - x1) * (y0 - y1)));
                }
            }
        }
        return (res == INT_MAX? 0 : res);
    }
};

 

上一篇:Matlab求切线和法平面(surfnorm,jacobian)


下一篇:【java细节】for循环,for-each(增强for循环),forEach以及Iterator迭代器对比