简体   繁体   English

如何在O(n)中的排序数组中找到两个总和为给定数字的数字?

[英]How to find two number whose sum is given number in sorted array in O(n)?

public static void findNumber(int number) {
    int[] soretedArray = { 1, 5, 6, 8, 9 };
    for (int i = 0; i <= soretedArray.length; i++) {
        for (int j = i + 1; j < soretedArray.length; j++) {
            if (soretedArray[i] + soretedArray[j] == number) {
                System.out.println(soretedArray[i] + "::" + soretedArray[j]);
                return;
            }
        }
    }
}

Using this code I am able to find the number and its complexity is O(N^2) but I have to find this using O(N) complexity ie using only one for loop or hash-map or similar in Java. 使用此代码,我能够找到数字,并且其复杂度为O(N ^ 2),但是我必须使用O(N)复杂度来找到该数字,即,仅使用一个for循环或哈希映射或Java中的相似性。

I remember, I was watching the official Google video about this problem. 我记得我当时在观看有关此问题的Google官方视频。 Although it is not demonstrated in java, it is explained step-by-step in different variations of the problem. 尽管未在Java中进行演示,但已通过问题的不同变型逐步介绍了它。 You should definitely check it: 您绝对应该检查一下:

How to: Work at Google — Example Coding/Engineering Interview 作法:在Google工作-编码/工程面试范例

As explained in the Google video that Alexander G is linking to , use two array indexes. Alexander G链接到的Google视频所述 ,请使用两个数组索引。 Initialize one to the first element (0) and the other to the last element ( sortedArray.length - 1 ). 初始化一个到第一个元素(0),另一个初始化到最后一个元素( sortedArray.length - 1 )。 In a loop, check the sum of the two elements at the two indexes. 循环检查两个索引处两个元素的总和。 If the sum is the number you were looking for, you're done. 如果总和是您要寻找的数字,那么您就完成了。 If it's too high, you need to find a smaller number at one of the indexes; 如果它太高,则需要在其中一个索引中找到较小的数字; move the right index one step to the left (since the array is sorted, this is the right way). 将右索引向左移动一步(由于数组已排序,因此是正确的方法)。 If on the other hand, the sum you got was too low, move the left index to the right to obtain a higher first addend. 另一方面,如果您得到的总和太低,请向左移动索引以获取较高的第一加数。 When the two indexes meet, if you still haven't found the sum you were looking for, there isn't any. 当两个索引相遇时,如果仍然找不到要查找的总和,则没有任何索引。 At this point you have been n - 1 times through the loop, so the algorithm runs in O(n). 此时,您已完成n - 1次循环,因此算法以O(n)运行。

We ought to first check the precondition, that the array is really sorted. 我们应该首先检查先决条件,即数组是否已真正排序。 This too can be done in O(n), so doing it doesn't break any requirements. 这也可以在O(n)中完成,因此这样做不会违反任何要求。

The algorithm may need refinement if you are required to find all possible pairs of numbers that yield the desired sum rather than just one pair. 如果您需要查找产生期望总和的所有可能数字对,而不是一对,则算法可能需要改进。

Is this answer superfluous when the video link has already said it? 视频链接已经说过了,这个答案是多余的吗? For one thing, my explanation is shorter, so if it suffices, you're fine. 一方面,我的解释要短一些,所以只要足够,您就可以了。 Most importantly, if the video is removed or just moved to another URL, my answer will still be here. 最重要的是,如果视频被删除或仅移至另一个URL,我的答案仍将在此处。

With fixed number , for any chosen x in the array you just have to find if number-x is in the array (Note that you can also bound x ). 使用固定的number ,对于数组中任何选择的x ,您只需要查找number-x是否在数组中(请注意,您也可以绑定x )。 This will not give you O(n), but O(n.log(n)). 这不会给您O(n),而是O(n.log(n))。

Maybe by remarking that if you have a_i and a_j ( j>i ), taking the sum and comparing against number , if the result is greater next interesting tests are with a_(i-1) or a_(j-1) , and if result is lower next interesting tests are with a_(i+1) or a_(j+1) , will give hint to linear-time? 也许通过指出如果您有a_ia_jj>i ),取和并与number进行比较,如果结果更大,则下一个有趣的测试是使用a_(i-1)a_(j-1) ,如果结果是较低的下一个有趣的测试是使用a_(i+1)a_(j+1) ,是否会提示线性时间?

暂无
暂无

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

相关问题 如何在 O(n) 时间复杂度中找到总和等于特定数字的连续子数组 - How to find continuous sub-array whose sum is equal to a particular number in O(n) time complexity 如何在O(log(N))时间内找到排序数组中在一定范围内的整数数? - How to find number of integers in a sorted array that are within a certain range in O(log(N)) time? 如何在整数数组中找到所有对,其和等于给定数字 - How to Find all Pairs in an Array of Integers Whose sum is Equal to a Given Number 如何找到总和等于或小于给定数量的元组数? - How to find number of tuples whose sum is equal or less than a given number? 高效算法在O(log(N))时间内找到排序数组中在一定范围内的整数数量? - Efficient algo to find number of integers in a sorted array that are within a certain range in O(log(N)) time? 在 java 中通过 hashmap 从数组中查找所有元素对,其总和等于给定数 - Find all pairs of elements from an array whose sum is equal to given number by hashmap in java 以O(log n)复杂度计算排序数组中值的出现次数 - Count the number of appearances of a value in a sorted array in O(log n) complexity java-查找总和为给定数字的整数序列 - java - find sequence of integers whose sum is that of a given number 在数组中找到总和等于给定数字的所有数字对? - finding all pairs of numbers in an array whose sum equals a given number? 从总和等于给定数字的数组中计算对? - Count pairs from an array whose sum is equal to a given number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM