【发布时间】:2017-11-06 23:58:19
【问题描述】:
我想知道是否有更好的方法,给定一个纬度和一个经度,找到最近的公共汽车站,而不是通过完整的 ArrayList:
这就是 BusStation 类:
while(iteratore.hasNext()){ // for the full array size...
tmp = (BusStation) iteratore.next();
tmpDistance = getDistance(currentLat, currentLong, tmp.getStopLat(), tmp.getStopLon());
if(tmpDistance < nearestDistance){ // if the distance is smaller ...
nearestStop = tmp; // i save the bus stop
nearestDistance = tmpDistance; // i save the distance too
}
}
这是效率不高的代码,它工作,但每次调用该方法时都必须查看具有 n 复数的完整数组。
我们如何通过 数据结构 作为 二叉搜索树 来优化它?
方法的签名应该是这样的:
public BusStation search(double currentLat, double currentLong){}
【问题讨论】:
-
可以使用Haversine公式
-
到目前为止,您自己尝试过哪些优化?
-
您可以对
ArrayList进行排序,然后将其与您的值进行比较。一旦你找到了最佳距离,你就可以休息了。这会将您的复杂性从O(n^2)更改为O(n log n) -
@Nehorai Haversine 公式没有帮助。它为您提供球体上坐标之间的距离。但不能帮助您有效地找到最小的。
-
也许这个问题的答案中的一种方法可以帮助stackoverflow.com/questions/1901139/… 另外,您可以添加您的
getDistance()方法实现,因为它很可能会在搜索优化中发挥作用。
标签: java optimization data-structures binary-search-tree latitude-longitude