【问题标题】:Find nearest double in ordered ArrayList<Double>在有序 ArrayList<Double> 中查找最近的双精度
【发布时间】:2018-01-20 16:04:13
【问题描述】:

我有一个已排序的 ArrayList,我想在其中找到最接近的元素以使 x 加倍。

double x = 50.2;
ArrayList<Double> arrayList = new ArrayList<>();
arrayList.add(12.34);
arrayList.add(86.00);
arrayList.add(87.26);
arrayList.add(241.63);
...

double findNearestDoubleInList(list, x)
{
   ...
}

我该如何做到这一点?

【问题讨论】:

  • 研究 binarySearch 并使用它。这就是答案
  • 请提供find nearest element to double的例子!
  • 我们无法为您提供答案,您必须提供您尝试过的内容,我们可以从那里提供帮助。
  • 你不能直接实现二分搜索,但是对逻辑的一些改变会得到你想要的东西

标签: java sorting arraylist


【解决方案1】:

使用java.lang.Math.abs 获取列表中的值与所需值之间的差值并找到最接近的值。

    double answer = arrayList.get(0);
    double current = Double.MAX_VALUE;
    for (Double value : arrayList) {
        if (Math.abs(value - x) < current) {
            answer = value;
            current = Math.abs(value - x);
        }
    }

【讨论】:

  • 感谢您的澄清。
  • 列表排序时为什么要使用最差的 O(n) 复杂度?正确的答案是 binarySearch,它利用@Adem 建议的排序列表,因此产生的复杂度将是 O(log n)。
  • @matoni,他不关心性能,他只是想要一个方法,实现二分查找,逻辑上应该做一些改动,他不能直接应用
【解决方案2】:

遍历列表并计算x 与列表中当前元素的绝对差。

返回最小的绝对差。

static double findNearestDoubleInList(ArrayList<Double> list, double d){
    if(list.size() == 0){
        return -1;
    }

    if(list.size() == 1){
        return list.get(0);
    }

    double current = list.get(0);
    double currentMin = Math.abs(list.get(0) - d);

    for(int i = 1; i < list.size(); i ++){

        double difference = Math.abs(list.get(i) - d);

        if(currentMin > difference){
            currentMin = difference;

            current = list.get(i);
        }
    }

    return current;
}

对于这样的算法,始终假设第一个元素是您的解决方案。

【讨论】:

    【解决方案3】:
    // Java program to find element closet to given target by binary search on the sorted array
    
    
    class FindClosestNumber {
    
        // Returns element closest to target in arr[]
        public static double findClosest(double[] arr, double target)
        {
            int n = arr.length;
    
            // Corner cases
            if (target <= arr[0])
                return arr[0];
            if (target >= arr[n - 1])
                return arr[n - 1];
    
            // Doing binary search
            int i = 0, j = n, mid = 0;
            while (i < j) {
                mid = (i + j) / 2;
    
                // if arr[mid] and target are very close 
                if (Math.abs(arr[mid]-target) < 0.0001)
                    return arr[mid];
    
                /* If target is less than array element,
                   then search in left */
                if (target < arr[mid]) {
    
                    // If target is greater than previous
                    // to mid, return closest of two
                    if (mid > 0 && target > arr[mid - 1])
                        return getClosest(arr[mid - 1],
                                arr[mid], target);
    
                    /* Repeat for left half */
                    j = mid;
                }
    
                // If target is greater than mid
                else {
                    if (mid < n-1 && target < arr[mid + 1])
                        return getClosest(arr[mid],
                                arr[mid + 1], target);
                    i = mid + 1; // update i
                }
            }
    
            // Only single element left after search
            return arr[mid];
        }
    
        // Method to compare which one is the more close
        // We find the closest by taking the difference
        //  between the target and both values. It assumes
        // that val2 is greater than val1 and target lies
        // between these two.
        public static double getClosest(double val1, double val2,
                                     double target)
        {
            if (target - val1 >= val2 - target)
                return val2;
            else
                return val1;
        }
    
        // Driver code
        public static void main(String[] args)
        {
            double arr[] = {12.34, 86.00, 87.26, 241.63};
            double target = 50.2;
            System.out.println(findClosest(arr, target));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-19
      • 2019-01-14
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多