1

可能重复:
从数组中找到一对元素,其和等于给定数字

我最近收到了以下 Java 面试问题。

目标是仅通过输入数组一次即可完成方法任务。

我声称一次通过阵列是不可能完成这项任务的,但我遇到了通常的沉默,停顿,然后面试官宣布面试结束而没有给我答案。

public class SortedArrayOps {  
    public SortedArrayOps() {  

    }  

//    Print at the system out the first two ints found in the sorted array: sortedInts[] whose sum is equal to Sum in a single pass over the array sortedInts[] with no 0 value allowed.  
//  i.e. sortedInts[i] + sortedInts[?] = Sum where ? is the target index to be found to complete the task.  
static void PrintIntSumValues(int Sum, int sortedInts[]) {  
//        need to test to see if the Sum value is contained in the array sortedInts. And, if not do nothing.  
    for(int i=0; i<sortedInts.length; i++) {  
//            ... do some work: algebra and logic ...  
//            System.out.println sortedInts[i]+sortedInts[?] sums to Sum.  
    }  
}  

public static void main(String[] args) {  
    final int[] sortedArray = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50};
    PrintIntSumValues(48, sortedArray);  
}  

}

4

5 回答 5

1

我不确定您要查找数组中的哪些值(“前两个”整数是什么意思?它们的索引的最小总和?一个是最小的?任何两个碰巧首先弹出?),但这个解决方案是 O( n),通过一次,不使用额外的数据结构,并且只使用额外的一个 int。它并不总是找到最接近的两个索引,也不总是找到“第一个”,无论这可能意味着什么。我相信它总会找到总和最小的两个(直到你们找到一个反例)。

如果你们发现任何错误,请告诉我:

class Test {

    public static void main(String[] args) {  
        int[] sortedArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        PrintIntSumValues(6, sortedArray);

        sortedArray = new int[] {1, 2,3, 12, 23423};
        PrintIntSumValues(15, sortedArray);


        sortedArray = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        PrintIntSumValues(100, sortedArray);

        sortedArray = new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50};
        PrintIntSumValues(48, sortedArray);  
    }

    //    Print at the system out the first two ints found in the sorted array: sortedInts[] whose sum is equal to Sum in a single pass over the array sortedInts[] with no 0 value allowed.  
    //  i.e. sortedInts[i] + sortedInts[?] = Sum where ? is the target index to be found to complete the task.  
    static void PrintIntSumValues(int Sum, int sortedInts[]) {  
        // need to test to see if the Sum value is contained in the array sortedInts. And, if not do nothing.  
        int offset = sortedInts.length-1;

        for(int i=0; i<sortedInts.length; i++) {  
            //            ... do some work: algebra and logic ...   
            if ((sortedInts[i] + sortedInts[offset]) == Sum){
                System.out.println("sortedInts[" + i + "]+sortedInts[" + offset + "] sums to " + Sum + ".");
                return;
            } else {
                int remaining = Sum - sortedInts[i];
                if (remaining < sortedInts[i] ){
                    // We need something before i
                    if (remaining < sortedInts[offset]) {
                        // Even before offset
                        offset = 0 + (offset - 0)/2;
                    } else {
                        // Between offset and i
                        offset = offset + (i - offset)/2;
                    }
                } else {
                    // We need something after i
                    if (remaining < sortedInts[offset]) {
                        // But before offset
                        offset = i + (offset - i)/2;
                    } else {
                        // Even after offset
                        offset = offset + (sortedInts.length - offset)/2;
                    }
                }
            }
        }  
        System.out.println("There was no sum :(");

    }  
}

您可以在此处查看输出。

于 2012-07-31T03:50:00.183 回答
0

这应该有效。您有两个指针,并且只对数据进行一次传递。

j = sortedInts.length - 1;
for(int i=0; i<sortedInts.length && j>=i; i++) {  
    sx = sortedInts[i];
    while (sx + sortedInts[j] > Sum)
        j++;
    if (sx + sortedInts[j] == Sum)
        ...
}
于 2012-07-31T10:59:16.940 回答
0

导入 java.util.HashMap;

            public class SortedArrayOps {

                public SortedArrayOps() {
                }

            //    Print at the system out the first two ints found in the sorted array: sortedInts[] whose sum is equal to Sum in a single pass over the array sortedInts[] with no 0 value allowed.
            //  i.e. sortedInts[i] + sortedInts[?] = Sum where ? is the target index to be found to complete the task.
            static void PrintIntSumValues(int Sum, int sortedInts[]) {
                HashMap<Integer, Boolean> pool= new HashMap<Integer, Boolean> ();
                for(int i=0; i<sortedInts.length; i++) {
                    int current = sortedInts[i];
                    int target = Sum - current;
                    if (pool.containsKey(target)) {
                        System.out.println(String.format("%d and %d sum to %d", current, target, Sum));
                        break;
                    }
                    else {
                        pool.put(current, Boolean.TRUE);
                    }
                }
            }

            public static void main(String[] args) {
                final int[] sortedArray = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50};
                PrintIntSumValues(48, sortedArray);
            }
            }
于 2012-07-31T02:31:45.127 回答
0

这是使用 a 的完整解决方案HashMap

import java.util.HashMap;

public class Test
{
    public static void main(String[] args)
    {
        final int[] sortedArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int sum = 6;

        printSum(sum, sortedArray);
    }

    private static void printSum(int sum, int[] sortedArray)
    {
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();

        for (int index = 0; index < sortedArray.length; index++)
        {
            int currentNumber = sortedArray[index];
            int remainder = sum - currentNumber;

            if (map.containsKey(remainder))
            {
                System.out.println(String.format("%d + %d = %d", currentNumber, remainder, sum));

                break;
            }
            else
            {
                map.put(currentNumber, index);
            }
        }
    }
}
于 2012-07-31T02:49:46.203 回答
-2

因为值的数组是特定的,所以解决方案可以简化为,

public class SortedArrayOps {
public SortedArrayOps() {

}

// Print at the system out the first two ints found in the sorted array:
// sortedInts[] whose sum is equal to Sum in a single pass over the array
// sortedInts[] with no 0 value allowed.
// i.e. sortedInts[i] + sortedInts[?] = Sum where ? is the target index to
// be found to complete the task.
static void PrintIntSumValues(int Sum, int sortedInts[]) {
    // need to test to see if the Sum value is contained in the array
    // sortedInts. And, if not do nothing.
    for (int i = 0; i < sortedInts.length; i++) {
        // ... do some work: algebra and logic ...
        // System.out.println sortedInts[i]+sortedInts[?] sums to Sum.
        int remainder = Sum - sortedInts[i];
        if( remainder <= sortedInts.length && remainder>0 && remainder!=sortedInts[i]) {
            System.out.print(String.format("%d + %d = %d", sortedInts[i], sortedInts[remainder-1], Sum));
            break;
        }
    }
}

public static void main(String[] args) {
    final int[] sortedArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
            14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
            30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
            46, 47, 48, 49, 50 };
    PrintIntSumValues(48, sortedArray);
}

}

于 2012-07-31T04:27:03.327 回答