新农上路
- 积分
- 96
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2016-10-16
- 最后登录
- 1970-1-1
|
写了下code,必须得去重
- public class SmallestRectangle {
- public static void main(String[] args) {
- SmallestRectangle ss = new SmallestRectangle();
- int[][] points = {{0,0}, {1,0}, {0,1}, {1,2}, {2,0}, {2,1}};
- System.out.println(ss.findSmallestRectangle(points));
- }
- public int findSmallestRectangle(int[][] points) {
- int res = Integer.MAX_VALUE;
- if (points == null || points.length < 4) {
- return res;
- }
- HashSet<String> set = new HashSet<>();
- for (int[] p : points) {
- set.add(p[0] + "#" + p[1]);
- }
- for (int i = 0; i < points.length; i++) {
- for (int j = i + 1; j < points.length; j++) {
- String p1 = points[i][0] + "#" + points[i][1];
- String p2 = points[j][0] + "#" + points[j][1];
- String p3 = points[i][0] + "#" + points[j][1];
- String p4 = points[j][0] + "#" + points[i][1];
- set.remove(p1);
- set.remove(p2);
- if (set.contains(p3)
- && set.contains(p4)) {
- int area = Math.abs(points[i][0] - points[j][0]) * Math.abs(points[i][1] - points[j][1]);
- res = Math.min(area, res);
- }
- set.add(p1);
- set.add(p2);
- }
- }
- return res;
- }
- }
复制代码 |
|