【问题标题】:Find to closest higher and lower number in an array在数组中查找最接近的较高和较低数字
【发布时间】:2014-06-09 10:35:15
【问题描述】:

嘿嘿,

我实际上是在尝试实现一个以整数作为输入的函数。 我还有一个升序整数数组。

现在我尝试找到最接近我的单个整数的较低和最接近较高的数字。

我想将它作为一个数组返回,但我只找到了一种解决方案来找到最接近给定输入的数字。

public int getClosestTimeValue(int time) {
int nearest = -1;
int bestDistanceFoundYet = Integer.getInteger(null);
int[] array = null;     
// We iterate on the array...
for (int i = 0; i < array.length; i++) {
     // if we found the desired number, we return it.
if (array[i] == time) {
    return array[i];
    } else {
        int d = Math.abs(time - array[i]);
if (d < bestDistanceFoundYet) {
    nearest = array[i];
}
    }
}
    return nearest;
}

有谁知道如何在 java 中解决这个问题?

谢谢你,卢卡斯

【问题讨论】:

  • 这段代码不是你的,对吧?
  • 当您说“上升”时,您的意思是排序吗?如果是这样,您可以使用二进制搜索来查找数组中的数字。
  • your bestDistanceFoundYet 始终为空.. 你必须使用它..
  • 旁注:撇号是这个字符:' 不是这个:`
  • 您可以使用 Java Map 可能是 TreeMap。使用 TreeMap 的 floor 和 ceiling 方法(也更低和更高)。你可以在几行代码中做到这一点

标签: java arrays sorting compare


【解决方案1】:

如果您不需要直接使用数组,那么您可以使用 NavigableSet 和 ceiling()/floor() 方法来获取集合中最近的较大/较小元素。示例:

NavigableSet<Integer> values = new TreeSet<Integer>();
for (int x : array) { values.add(x); }
int lower = values.floor(time);
int higher = values.ceiling(time);

如果您需要使用数组(作业?),那么请找到一个很好的二分搜索参考。

【讨论】:

    【解决方案2】:

    目前您只搜索一次。要找到最接近的较低时间和最接近的较高时间,您应该有两个变量。然后您可以检查迭代时间是否低于或高于输入并将值存储在相应的变量中。同样,此时您只返回一个值,但为了返回多个值,您应该通过数组来完成。

    我不确定它是否能回答您的问题,但我将如何解决这个问题:

    array = new int[]; // Array of times you have declared elsewhere.
    
    // Method which returns the array of found times.
    public int[] getClosestTime(int time) {
        int closestLowerTime = 0;
        int closestHigherTime = 100; // Value bigger than the largest value in the array.
        times = new int[2]; // Array for keeping the two closest values.
        // Iterating the array.
        for (int i = 0; i < array.length; i++) {
            // Finding the two closest values.
            int difference = time - array[i];
            if (difference > 0 && array[i] > closestLowerTime) {
                closestLowerTime = array[i];
            } else if (difference < 0 && array[i] < closestHigherTime) {
                closestHigherTime = array[i];
            }
        }
        times[0] = closestLowerTime;
        times[1] = closestHigherTime;
        return times;
    }
    

    这会找到最接近的较低值和较高值,并将它们作为数组返回。目前我解决了它,因为时间在 0 到 100 之间,但如果您不知道最大时间值,您可以通过另一个循环找到它,该循环遍历数组并将最大值存储在 closestHigherTime 中。我没有找到通过数组返回确切值的正确方法,但这是必需的吗?

    【讨论】:

      【解决方案3】:

      随着数组的排序......

      1) Check the middle two elements ..if both are less than the number check the left half (.i.e repeat step1)
      else if both are greater than the number repeat step1 for right half...else the selected two numbers are your required answer
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-30
        • 1970-01-01
        • 2012-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多