返回值数组中的两个最大整数

我试图从我的int数组返回两个最大的整数。 我能够返回最大和最小的罚款,但我不能让我的算法返回最大的两个。 这里非常感谢任何帮助。

请原谅我的代码中的任何错误。 这是一个练习课程,问题来自去年大学的考试材料。

这是我的代码:

public class TwoLargestIntsArray { public static void main(String [] args){ int [] values = new int[5]; values[0] = 5; values[1] = 10; values[2] = 15; values[3] = 20; values[4] = 25; System.out.println(twoLargest(values)); System.out.println(); } public static int twoLargest(int values[]){ int largestA = values[0]; int largestB = values[0]; for(int i = 0; i  largestA){ largestA = values[i]; } if(values[i] < largestA){ largestB = values[i]; } } return largestA + largestB; } } 

你可以写

 public static int[] twoLargest(int values[]){ int largestA = Integer.MIN_VALUE, largestB = Integer.MIN_VALUE; for(int value : values) { if(value > largestA) { largestB = largestA; largestA = value; } else if (value > largestB) { largestB = value; } } return new int[] { largestA, largestB }; } 
 public static void twoLargest(int values[]){ int largestA = values[0]; int largestB = -1; for(int i = 0; i < values.length; i++){ if(values[i] > largestA){ largestB = largestA; largestA = values[i]; } else if (values[i] > largestB && values[i] != largestA) { largestB = values[i]; } } System.out.println("Largest - " + largestA); System.out.println("2nd largest Largest - " + largestB); } 
 public class Test { public static int[] findTwoHighestDistinctValues(int[] array) { int max = Integer.MIN_VALUE; int secondMax = Integer.MIN_VALUE; for (int value:array) { if (value > max) { secondMax = max; max = value; } else if (value > secondMax && value < max) { secondMax = value; } } return new int[] { max, secondMax }; } public static void main(String []args) { int [] values = new int[5]; values[0] = 5; values[1] = 10; values[2] = 15; values[3] = 20; values[4] = 25; int []ar = findTwoHighestDistinctValues(values); System.out.println("1 = "+ar[0]); System.out.println("2 = "+ar[1]); } } 

OUTPUT:

1 = 25

2 = 20

你不能让一个函数返回2个值。 您必须将它们包装在数组中,或使用引用参数。

传递数组以填充值:

 public static void twoLargest(int [] values, int [] ret){ //... ret[0] = largestA; ret[1] = largestB; } int [] ret = new int [2]; twoLargest(values, ret); // now ret[0] is largestA // and ret[1] is largestB 

试试这个:

 public static int[] twoLargest(int values[]){ int[] result = new int[2]; int largestA = 0; int largestB = 0; for(int i = 0; i < values.length; i++){ if(values[i] > largestA){ largestB = largestA; largestA = values[i]; } } result[0] = largestA; result[1] = largestB; return result; } 

我假设它可以帮助你,以防你能够从一个函数获得最大的,第二大的,第三大的等等。 我创造了这样一个:

 public static int xLargest(int values[], int largeIndex) 

只需传递数组和largeIndex,最大发送1,第二大发送2,依此类推。

 import java.util.ArrayList; import java.util.Collections; import java.util.List; public class XLargestIntArray { public static void main(String[] args) { int[] values = new int[5]; values[0] = 5; values[1] = 10; values[2] = 15; values[3] = 20; values[4] = 25; System.out.println(xLargest(values,2)); System.out.println(); } public static int xLargest(int values[], int largeIndex) { List intList = new ArrayList(); for (int index = 0; index < values.length; index++) { intList.add(values[index]); } Collections.sort(intList); return intList.get(intList.size() - largeIndex); } } 

您还可以使用嵌套类来存储计算结果。 例如:

  private static class Result { int largestA; int largestB; Result(int largestA, int largestB) { this.largestA = largestA; this.largestB = largestB; } } 

然后接收如下数据:

 Result result = twoLargest(values); System.out.println(result.largestA); System.out.println(result.largestB); 

@Nilesh Jadav的回答涵盖所有案例。 回答@Peter Lawrey在数组具有最大值的情况下失败是最后一个元素。 例如[10,2,5,1,8,20]使用已接受的解决方案返回20和8。

  public static void main(String[] args) { int[] numbers = new int[]{1, 2, 5, 4, 57, 54, 656, 4}; int temp = numbers[0]; int max = temp; int lastMax = temp; for (int index = 1; index < numbers.length; index++){ int currentIndex = numbers[index]; // check any time is it bigger than current maximum value max = Math.max(max, currentIndex); // if is grow up then should be max != temp; set old Max in last or // is not group up but current index is bigger then last index update last max value lastMax = max != temp ? temp : Math.max(currentIndex, lastMax); temp = max; } System.out.println("Last max: " + lastMax); System.out.println("Max:" + max); } 

试试吧

 int [] array1={10,2,5,1,8,20}; int largest= array1[0]; int seclargest=array1[0]; for (int i=1;i 
 private static void printTwoMaxNumberWithoutSortMethod(int[] input) { int max=0,lastMax=0; lastMax=input[0]; max=input[0]; for(int i=1;i
		      	
  //Java8&9 makes this easier with a cleaner code int[] numbers = new int[]{1, 2, 5, 4, 57, 54, 656, 4}; int maxSize = 2; Arrays.stream(numbers) //stream .boxed() //to Integer Object .sorted(Comparator.reverseOrder()) //sorted .limit(maxSize ) //keep N values .forEach(System.out::println); 

NB。 我们可以使用任何实现可比较的bean对象或使用相关的自定义比较器。

 public class TwoLargestIntArray { public static void main(String [] args){ int a[] = new int[5]; a[0] = 110; a[1] = 50; a[2] = 15; a[3] = 30; a[4] = 60; System.out.println(twoLargest(a)); System.out.println(); } public static int twoLargest(int a[]){ int firstMax = 0; int secondMax = 0; for(int i = 0; i < a.length; i++){ if(a[i]>firstMax) { secondMax=firstMax; firstMax = a[i];} else if (a[i]>secondMax) { secondMax= a[i]; } } return firstMax + secondMax; }} 

如果性能不是问题,它不应该在小型arrays上,这可以用更少的代码完成。

最直接的解决方案是简单地对数组进行排序并返回其最后一个值,并返回到最后一个值:

 public static int[] twoLargest(int[] values) { Arrays.sort(values); return new int[]{values[values.length - 1], values[values.length - 2]}; } 

上面代码的时间复杂度是O(n log(n)) ,如Javadoc for Arrays.sort()

实施说明:排序算法是Vladimir Yaroslavskiy,Jon Bentley和Joshua Bloch的双枢轴快速算法。 该算法在许多数据集上提供O(n log(n))性能,导致其他快速排序降级为二次性能,并且通常比传统(单枢轴)Quicksort实现更快。

如果预期输入是一个少于两个元素的数组,则需要添加一些error handling,例如抛出exception。

 public static int Compute(int _arrValues[]){ int _intX = _arrValues[0]; int _intY = _arrValues[1]; for(int i = 2; i < _arrValues.length; i++){ if(_arrValues[i] > _intX){ _intX= values[i]; } else if(_arrValues[i] > _intY){ _intY = values[i]; } } return _intX + _intY; }