【问题标题】:Find min 3 values and max 3 values in array in Java在 Java 中的数组中查找最小 3 个值和最大 3 个值
【发布时间】:2022-10-14 21:16:03
【问题描述】:

我有一个关于在数字数组中找到 3 个最小值和 3 个最大值的问题。即使我在数组中正确找到了所有这 3 个最大值,我也无法获得 min3 值。

这是 nums 数组[-100,-98,-1,2,3,4]

min3 通常为 -1,但我得到 4。

我该如何解决?

这是下面显示的代码sn-p。

int max1 = Integer.MIN_VALUE;
int max2 = Integer.MIN_VALUE;
int max3 = Integer.MIN_VALUE;
        
int min1 = Integer.MAX_VALUE;
int min2 = Integer.MAX_VALUE;
int min3 = Integer.MAX_VALUE; 
        
if(nums.length >= 3) {
            
    for(Integer value : nums) {
        int current = value;
                
                
        // Max
        if(current > max1) {
            max3 = max2;
            max2 = max1;
            max1 = current;
        }else if(current > max2){
            max3 = max2;
            max2 = current;
        }else {
            max3 = current;
        }
                
        // min
        if(min1 > current) {
            min3 = min2;
            min2 = min1;
            min1 = current;
        }
        else if(min2 > current) {
            min3 = min2;
            min2 = current; 
        }else {
            min3 = current; 
        }
                
    }
            
    System.out.println("max1 : " + max1 + " , max2 : " + max2 + " , max3 : " + max3);
    System.out.println("min1 : " + min1 + " , min2 : " + min2 + " , min3 : " + min3);
                
}

这是如下所示的控制台输出。

max1 : 4 , max2 : 3 , max3 : 2
min1 : -100 , min2 : -98 , min3 : 4

【问题讨论】:

  • nums 中包含什么?
  • 你为什么每次都做max3 = current;?没有if 可以对照current 检查值。
  • 否则 { max3 = 当前;你真的应该检查你是否需要在这里覆盖 max3
  • @f1sh max3 = current; 仅对 else 情况有效。
  • @leonardkraemer 我编辑了我的帖子以显示 nums 数组。

标签: java arrays


【解决方案1】:

你不检查current < min3

【讨论】:

  • 所以对于current>max3
【解决方案2】:

这是下面的解决方案。

if(nums.length >= 3) {
            
            for(Integer value : nums) {
                int current = value;
                
                
                // Max
                if(current > max1) {
                    max3 = max2;
                    max2 = max1;
                    max1 = current;
                }else if(current > max2){
                    max3 = max2;
                    max2 = current;
                }else if(current > max3){
                    max3 = current;
                }
                
                // min
                if(min1 > current) {
                    min3 = min2;
                    min2 = min1;
                    min1 = current;
                }
                else if(min2 > current) {
                    min3 = min2;
                    min2 = current; 
                }else if(min3 > current){
                    min3 = current; 
                }
                
            }
            
            System.out.println("max1 : " + max1 + " , max2 : " + max2 + " , max3 : " + max3);
            System.out.println("min1 : " + min1 + " , min2 : " + min2 + " , min3 : " + min3);
            
        }

【讨论】:

    【解决方案3】:

    问题的解决方案“找到 N 最大值 / N 最小值”基于 N 个变量和手动实现的条件逻辑不灵活并且容易出错。

    如果您需要找到,比如说,10 最小/最大值而不是3,它将变得完全无法管理。

    您可以维护两个PriorityQueues,而不是一堆变量。在尝试将新元素添加到队列之前,您需要检查它的大小并将下一个元素与队列的根元素进行比较。

    这就是它的实现方式:

    public static void main(String[] args) {
        int[] arr = {8, 5, 7, 8, -3, 5, -5, 9, 12, 18};
    
        Queue<Integer> min = new PriorityQueue<>(Comparator.reverseOrder());
        Queue<Integer> max = new PriorityQueue<>();
        
        for (int next: arr) {
            tryAdd(min, 3, next);
            tryAdd(max, 3, next);
        }
    
        System.out.println(min);
        System.out.println(max);
    }
    
    public static void tryAdd(Queue<Integer> queue, Comparator<Integer> comp,
                              int size, int next) {
        
        if (queue.size() == size && comp.compare(next, queue.peek()) > 0) queue.poll();
        if (queue.size() < size) queue.add(next);
    }
    

    输出:

    [5, -3, -5]
    [9, 12, 18]
    

    【讨论】:

    • 最小值是错误的。
    • @S.N 抱歉,已修改。
    猜你喜欢
    • 2018-10-05
    • 1970-01-01
    • 2018-07-03
    • 2016-11-12
    • 2018-05-03
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    相关资源
    最近更新 更多