如何在具有正值和负值的数组中找到最大的负值?

How do I find the largest negative value in an array with both positive and negative values?

我需要return最大的负值,如果没有负值,我需要return归零。 这是我拥有的:

public int greatestNegative(int[] list) {


    for (int i = 0; i < list.length; i++) {


       if (list[i] < 0)
           negativeNumbers ++;
    }

    int j = list.length - 1;

    while (j >= 0) {
       if (list[j - negativeNumbers] < 0) {
        list[j] = 0;
        list[j - 1] = list[j - negativeNumbers];
        negativeNumbers--;
        j--;
       }
       else{
        list[j] = list[j - negativeNumbers];
        j--;
     }
  }

}

你一定要试试这个....

public int greatestNegative(int[] list) {
    int negNum = 0;
    for(int i=0; i<list.length; i++) {
        if(list[i] < negNum){
            negNum = list[i];
        }
    }
    return negNum;
}


public int largNegative(int[] list) {
    int negNum = 0;
    boolean foundNeg = false;
    for(int i=0; i<list.length; i++) {
        if(list[i] < negNum && !foundNeg){
            foundNeg = true;
            negNum = list[i];
        } else if(foundNeg && list[i] < 0 && negNum < list[i]) {
            negNum = list[i];
        }
    }
    return negNum;
}

这里是return最小负数

的代码
public static int greatestNegative(int[] list) {
        int negativeNumbers = 0;
        for (int i = 0; i < list.length; i++) {
           if (list[i] < 0 && list[i] < negativeNumbers)
               negativeNumbers  = list[i];
        }

        return negativeNumbers;
    }

Input : 1, 2, -3, 5, 0, -6

Output : -6

Input : 1, 2, 3, 5, 0, 6

Output : 0

只需在添加条件的情况下找到最大数量即可。

public static int greatestNegative(int[] list) {
    int max = Integer.MIN;
    boolean set = false;
    for (int i = 0; i < list.length; i++) {
        if (list[i] < 0 && list[i] > max) {
             max = arr[i];
             set = true;
        }
    }
    if (!set)
        max = 0;
    return max;
}

如果您需要最大的负数,则对数组进行精简排序搜索第一个负数

import java.util.Arrays;

class Main {

    public static void main(String[] args) {
        int arr[] = { 2, 4, 1, 7,2,-3,5,-20,-4,5,-9};
        System.out.println(greatestNegative(arr));
    }

    private static int greatestNegative(int[] arr) {
        Arrays.sort(arr);
        for (int i = arr.length - 1; i >= 0; i--) {
            if (isNegative (arr[i])) {
                return arr[i];
            }
        }
        return 0;
    }

    private static boolean isNegative (int i) {
        return i < 0;
    }
}

Output : -3

请检查以下代码,它将 首先从数组中计算小数, 然后检查是阳性吗?如果是 return 0 否则 return 否定。

public static int greatestNegative(int[] list) 
{
    int negativeNumbers = Integer.MAX_VALUE;
    for (int i = 0; i < list.length; i++) {
        if (list[i] < negativeNumbers)
                negativeNumbers  = list[i];
    }

    if(negativeNumbers  >=0)
         return 0;
    else
         return negativeNumbers;

}

首先将 "maxNegative" 值设置为 0。然后分配您遇到的第一个负数。之后,只分配更高的负数。如果没有负数,那么您的 "maxNegative" 仍将为零。

public static void main(String[] args) {
    int arr[] = {2, -1, 4, 1, 0, 7, 2, -3, 5, 9, -4, 5, -9};
    int maxNegative = 0;
    for (int i = 0; i < arr.length; i++) {
        if (maxNegative == 0 && arr[i] < maxNegative) {
            // Set the first negative number you come across
            maxNegative = arr[i];
        } else if (maxNegative < arr[i] && arr[i] < 0) {
            // Set greater negative numbers
            maxNegative = arr[i];
        }
    }
    System.out.println(maxNegative);
}

结果:

-1

Java 8

然后是流,允许您使用一行代码来完成此操作。

public static void main(String[] args) {
    int arr[] = {2, 4, 1, 0, 7, 2, -3, 5, 9, -4, 5, -9};
    int maxNegative = Arrays.stream(arr).filter(a -> a < 0).max().orElse(0);
    System.out.println(maxNegative);
}

结果:

-3

你只需要把这个问题想成2个步骤:

  1. 只考虑列表[]中的负值。
  2. 在负值循环中,如果 (result == 0) 或 (value > result) 则更新当前结果。

代码:

public int greatestNegative(int[] list) {
    int result = 0;
    for (int i = 0; i < list.length; i++) {
        if (list[i] < 0) {
            if (result == 0 || list[i] > result) {
                result = list[i];
            }
        }
    }
    return result;
}