简体   繁体   English

在数组中找到最小的对和

[英]Find smallest pair sum in array

This is what I've been trying to achieve:这就是我一直在努力实现的目标:

Input: arr[] = {1, 7, 2, 9, 6}输入:arr[] = {1, 7, 2, 9, 6}
The pair (1, 2) will have the minimum sum pair ie 1 + 2 = 3对 (1, 2) 将具有最小和对,即 1 + 2 = 3

Output: (1,2)=3 Output: (1,2)=3

  • Because the while loop is iterating once, I am not able to achieve the intended result.因为 while 循环迭代一次,所以我无法达到预期的结果。 Need some help !!需要一些帮助 !!
public class Find_Smallest_Pair_Sum_In_Array {

    public static void main(String[] args) {

        int[] arr = { 1, 7, 2, 9, 6 };
        StringBuilder strBuilder = new StringBuilder();

        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                strBuilder.append("(");
                strBuilder.append(arr[i]);
                strBuilder.append(",");
                strBuilder.append(arr[j]);
                strBuilder.append(")");
                strBuilder.append("=");
                strBuilder.append(arr[i] + arr[j]);
                strBuilder.append("\n");
            }
        }
           
        Scanner scan = new Scanner(strBuilder.toString()); 

        int first, next = 0; 
        int previous = 0;
        String newStr = "";
        
        while (scan.hasNextLine()) {
            String oneLine = scan.nextLine();
            int num = Integer.parseInt(oneLine.substring(oneLine.lastIndexOf('=') + 1, oneLine.length()));
                        
            first = num;            
            if(num > previous) {
                newStr = oneLine;
                System.out.println("if :::: " +newStr);

            } else if(num < previous) {
                newStr = oneLine;
                System.out.println("else if :::: " +newStr);

            }
            previous = first;
            
        }
        System.out.println(newStr);
    }
}

I think what you're basically is obtaining the smallest two numbers of an array I hope...我认为你基本上是获得我希望的数组的最小两个数字......

This is a nice trick, probably not the more algorithmically efficient way but it's quick and dirty:这是一个不错的技巧,可能不是算法效率更高的方法,但它又快又脏:

Arrays.sort(arr); 
//This will sort your array.
if(arr.length>1){
System.out.println(arr[0]);
System.out.println(arr[1]);
}

Will be your smallest two values.将是您最小的两个值。 and It will also handle situations where your length is lower than 2.它还将处理您的长度小于 2 的情况。

To find the smallest 2 numbers, try this:要找到最小的 2 个数字,请尝试以下操作:

int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;
for (int i : arr) {
  if (i < min1) {
    min2 = min1;
    min1 = i;
  } else if (i < min2) min2 = i;
}

Issue fixed with this simple solution:这个简单的解决方案解决了问题:

    public class Find_Smallest_Pair_Sum_In_Array {

       public static void main(String[] args) {

        int[] arr = { 1, 7, 2, 9, 6 };
        StringBuilder strBuilder = new StringBuilder();

        Arrays.sort(arr);
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                strBuilder.append("(");
                strBuilder.append(arr[i]);
                strBuilder.append(",");
                strBuilder.append(arr[j]);
                strBuilder.append(")");
                strBuilder.append("=");
                strBuilder.append(arr[i] + arr[j]);
                strBuilder.append("\n");
            }
        }

        Scanner scan = new Scanner(strBuilder.toString()); 
        String oneLine = "";
        int i = 0;
        while (scan.hasNextLine()) {
            oneLine = scan.nextLine();
            if(i == 0) {
                System.out.println(oneLine);
                break;
            } 
           i++; // This is not required though, but still. 
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM