如何找到数组中的两个元素该款项为k [英] How can I find two elements in an array that sum to k

查看:104
本文介绍了如何找到数组中的两个元素该款项为k的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你给出无序的整数数组为

  A = {3,4,5,1,4,2}

输入: 6
输出: {5,1},{4,2}

我怎么可以在O做到这一点(n)或O(log n)的。任何建议将AP preciated。

更新:
我们能否写出比这事更有效?

 的for(int i = 0; I< array.length-1;我++)
{
    如果(阵列[I] +阵列[I + 1] == 6)
        的System.out.println({+阵列[I] +,+阵列第[i + 1] +});
}


解决方案

如果存储在输入数组中的数字是唯一积极的话,我会创建K + 1的ArrayList元件的另一列K。其中k是多少,你需要他们加起来。
少于k个,只有两个数字可以加起来K(假设我们在处理正整数}或特殊情况{0,K}。
然后,我将通过输入数组的所有元素,并为每个INT米即小于或等于k我会带它的索引和索引添加到ArrayList中的K索引米阵列迭代。
然后我会遍历数组的K上半年为每个索引我有存储在它的一些整数我会找到互补指数[K-1],看看是否有任何价值。如果有那么那些是你对。
而顺便说一句,这是为O(n)。

 公共静态无效findElemtsThatSumTo(int数据[],INT K){
    列表arrayK [] =新名单[K + 1];
    的for(int i = 0; I< arrayK.length;我++)
        arrayK [I] =新的ArrayList<整数GT;();    的for(int i = 0; I< data.length;我++){
        如果(数据[1] - = k)的
            。arrayK [数据[I]加(I)
    }    的for(int i = 0; I< arrayK.length / 2;我++){
        如果(arrayK [I] .isEmpty()及!&放大器;!arrayK [K-1] .isEmpty())
        {
            对于(对象指数:arrayK [I])
                对于(对象otherIndex:arrayK [K-1])
                    的System.out.println(号从的indeces [+ index.toString()+,+ otherIndex.toString()+]加起来+ K +。);
        }
    }}

Suppose you are given an array of unsorted integers as

A = {3,4,5,1,4,2}

Input : 6 Output : {5,1}, {4,2}

How can I do this in O(n) or O(log n). Any suggestions will be appreciated.

Update: Can we write something more efficient than this?

for(int i=0;i<array.length-1;i++)  
{  
    if(array[i]+array[i+1]==6)  
        System.out.println("{"+array[i]+","+array[i+1]+"}");  
}  

解决方案

If the numbers stored in the input array are only positive then I'd create another array K of k+1 ArrayList elements. Where k is the number you need them to add up to. Only two numbers less than k can add up to k (assuming we deal with positive ints} or in special case {0,k}. Then I would iterate through all elements of input array and for each int m that is less or equal to k I'd take its index and add that index to the array of ArrayList K at index m. Then I would iterate through first half of the array K and for each index i that has some ints stored in it I would find complementary index [k-i] and see if there are any values in it. If there are then those are your pairs. And btw this is O(n).

public static void findElemtsThatSumTo( int data[], int k){
    List arrayK[]= new List[k+1];
    for(int i=0; i<arrayK.length; i++)
        arrayK[i]= new ArrayList<Integer>();

    for(int i=0; i<data.length; i++){
        if(data[i]<=k)
            arrayK[data[i]].add(i);
    }

    for(int i=0; i<arrayK.length/2; i++){
        if(!arrayK[i].isEmpty() && !arrayK[k-i].isEmpty())
        {
            for(Object index: arrayK[i])
                for(Object otherIndex: arrayK[k-i])
                    System.out.println("Numbers at indeces ["+index.toString()+", "+otherIndex.toString()+"] add up to "+k+".");
        }
    }

}

这篇关于如何找到数组中的两个元素该款项为k的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆