查看: 735| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

[其他] Closest Pair of Point代码有bug,希望得到指点(java)

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
本帖最后由 csy99 于 2021-8-14 15:49 编辑

算是非常经典的一道题:输入n个点的横纵坐标,求所有点对之中最短距离。基本思路就是分治法。自己实在debug不出来。有一个数据量非常大的case没有通过。请大家帮忙看看,谢谢!这里给出OJ的链接https://www.lintcode.com/problem/966/description


[i][i][i][/i][/i][/i][i][i][i][i][i]
  1. [/i][/i][/i][/i][/i][/i]
  2. [i][i][i][i][i][i]public class Solution {
  3.     /**
  4.      * @param x: the list of coordinate x
  5.      * @param y: the list of coordinate y
  6.      * @return: find the closest pair of points and return the distance
  7.      */
  8.     public double getClosestDistance(double[] x, double[] y) {
  9.         int n = x.length;
  10.         Point[] points = new Point[n];
  11.         for (int i = 0; i < n; i++)
  12.             points = new Point(x, y);
  13.         Arrays.sort(points, (a,b)->(Double.compare(a.x, b.x)));
  14.         return split(points, 0, n-1);
  15.     }


  16.     private double bruteForce(Point[] points, int start, int end) {
  17.         double min = Integer.MAX_VALUE;
  18.         for (int i = start; i <= end; i++)
  19.             for (int j = i+1; j <= end; j++)
  20.                 min = Math.min(min, Point.distance(points, points[j]));
  21.         return min;
  22.     }

  23.     private double split(Point[] points, int start, int end) {
  24.         int n = end-start+1;
  25.         if (n <= 3) return bruteForce(points, start, end);
  26.         Point midX = points[n/2];
  27.         double dl = split(points, start, start+n/2);
  28.         double dr = split(points, start+n/2+1, end);
  29.         double d = Math.min(dl, dr);
  30.         List<Point> middles = new ArrayList();
  31.         for (int i = start; i <= end; i++) {
  32.             if (Math.abs(points.x - midX.x) <= d)
  33.                 middles.add(points);
  34.         }
  35.         return Math.min(d, crossRegionMin(middles, d));
  36.     }

  37.     private double crossRegionMin(List<Point> points, double min) {
  38.         Collections.sort(points, (a,b)->(Double.compare(a.y, b.y)));
  39.         for (int i = 0; i < points.size(); i++) {
  40.             for (int j = i+1; j < points.size(); j++) {
  41.                 if (points.get(j).y - points.get(i).y >= min)
  42.                     break;
  43.                 if (Point.distance(points.get(i), points.get(j)) < min)
  44.                     min = Point.distance(points.get(i), points.get(j));
  45.             }
  46.         }
  47.         return min;
  48.     }
  49. }

  50. class Point {
  51.     double x, y;

  52.     public Point(double x_, double y_) {
  53.         x = x_;
  54.         y = y_;
  55.     }

  56.     public static double distance(Point a, Point b) {
  57.         return Math.sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
  58.     }
  59. } [/i][/i][/i][/i][/i][/i]
  60. [i][i][i][i][i][i]
复制代码

[/i][/i][/i][/i][/i]

上一篇:39. Combination Sum 检查了很久不知道代码哪里有问题。。。。。。。。。。。。。。。
下一篇:LC个人周赛打卡记录贴
🔗
 楼主| csy99 2021-8-17 11:45:16 | 只看该作者
全局:
顶一下帖子,希望不要沉了
回复

使用道具 举报

🔗
 楼主| csy99 2021-8-19 01:50:22 | 只看该作者
全局:
求各位大神指点一下呀
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表