对理解和与codaddict的算法算法、codaddict

由网友(真果粒)分享简介:我指的是下面的question之前有人问。而且,我很感兴趣,提到了有以下解决方案:I am referring to the following question that was asked before. And, I am interested in the following solution mention...

我指的是下面的question之前有人问。而且,我很感兴趣,提到了有以下解决方案:

I am referring to the following question that was asked before. And, I am interested in the following solution mentioned over there:

我想了解以下整数数组,我经过反复#5丢失,如下所示:

I am trying to understand it for the following integer array and I am lost after iteration #5 as shown below :

比方说,我们的整数数组为: {1,2,3,4,8,9,10} ,我们应该打印这些对的哪些总和等于12.因此,我试图分析一步一步会发生什么,如果我们运用上面的方法:

Let's say our integer array is : {1,2,3,4,8,9,10} and we should print those pairs for which sum is equal to 12. So, I tried to analyze step by step what happens if we apply the above mentioned approach:

                             Key             Value   
Iteration 1 : i = 0       (12-1) = 11          1 
Iteration 2 : i = 1       (12-2) = 10          2
Iteration 3 : i = 2       (12-3) = 09          3
Iteration 4 : i = 3       (12-4) = 08          4
Iteration 5 : i = 4       // pairs.containsKey is true here so printing 
                             input[i] = 8   

难道任何一个给我解释一下为什么我们要打印输入[I] = 08 pairs.get(输入[I]))这也是08年迭代#5以上?

Could any one explain me why are we printing input[i] = 08 and pairs.get(input[i])) which is also 08 in iteration #5 above?

其次我没有发现任何东西在网上就codaddict的算法有关。

Secondly I didn't find anything online as far as codaddict's algorithm is concerned.

推荐答案

有一个简单的介绍一下你引用了答案。要回答你的问题,

Have had a brief look at the answer you quoted. To answer your question,

为什么我们打印输入[I] = 08和pairs.get(输入[I])),这也是08年迭代#5以上?

why are we printing input[i] = 08 and pairs.get(input[i])) which is also 08 in iteration #5 above?

这是印刷输入[I] 8 pairs.get (输入[I])这意味着 pairs.get(8) 4

it is printing input[i] which is 8, and pairs.get(input[i]) which means pairs.get(8) which is 4.

有些事情,你需要知道的是,那块的Java code不是实施Codaddict的逻辑。它看起来有点像,但它是简单地做一些不同的东西:Codaddict是存储输入值作为重点,而指数值,而Java实现的存储(合计值)的关键和的值。

Something you need to know is, that piece of Java code is NOT implementing Codaddict's logic. It looks a bit a like but it is simply doing something different: Codaddict is storing the input value as key, and index as value, while that Java implementation is storing (sum-value) as key and value as value.

这是Java实现不理智的。它在做什么可以被简化为:

That Java implementation is not sane. What it is doing can be simplified to:

public static void printSumPairs(int []input, int sum){
    Set<Integer> previousInts = new HashSet<>();

    for (int i : input) {
        if (previousInts.contains(sum - i)) {
             System.out.print("" + (sum - i) + ", " + i);
        } else {
             previousInts.add(i);
        }
    }
}

这基本上达到相同的结果的Java实现了一套,和(我相信)是非常容易理解了。

This essentially achieve the same result as that Java impl, and (I believe) is much easier to understand.

不能与重复数很好地工作,但(这是相同的原始的Java implement执行)。但是其处理号重复实际上是一个简单的逻辑。

Doesn't work well with duplicated numbers though (which is the same for the original Java impl). However having a logic that handles number duplication is actually easy.

阅读全文

相关推荐

最新文章