简体   繁体   English

我在 Pascal 的三角形代码中面临的逻辑错误是什么?

[英]What is the logical error I am facing in Pascal's Triangle code?

Wrong Code:错误代码:

class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> finallist = new ArrayList<List<Integer>>();
        if (numRows == 1){
            List<Integer> list1 = new ArrayList<>();
            list1.add(1);
            finallist.add(list1);
            return finallist;
        }
        else if (numRows == 2){
            List<Integer> list1 = new ArrayList<>();
            List<Integer> list2 = new ArrayList<>();
            list1.add(1);
            list2.add(1);
            list2.add(1);
            finallist.add(list1);
            finallist.add(list2);
            return finallist;
        }
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        List<Integer> alist = new ArrayList<>();
        list1.add(1);
        list2.add(1);
        alist.add(1);
        list2.add(1);
        alist.add(1);
        finallist.add(list1);
        finallist.add(list2);
        for (int j = 3;j <= numRows;j++) {
            List<Integer> list3 = new ArrayList<>();
            list3.add(1);
            for (int i = 0; i < alist.size() - 1; i++) {
                list3.add(alist.get(i) + alist.get(i + 1));
            }
            list3.add(1);
            finallist.add(list3);
            alist.clear();
            alist.addAll(list3);
            list3.clear();
        }
        return finallist;
    }
}

Output shown: Input: 5 Output 显示:输入:5

Output: [[1],[1,1],[],[],[]] Output: [[1],[1,1],[],[],[]]

Expected: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]预期:[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

Correct Code:正确代码:

class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> finallist = new ArrayList<List<Integer>>();
        if (numRows == 1){
            List<Integer> list1 = new ArrayList<>();
            list1.add(1);
            finallist.add(list1);
            return finallist;
        }
        else if (numRows == 2){
            List<Integer> list1 = new ArrayList<>();
            List<Integer> list2 = new ArrayList<>();
            list1.add(1);
            list2.add(1);
            list2.add(1);
            finallist.add(list1);
            finallist.add(list2);
            return finallist;
        }
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        ArrayList<Integer> alist = new ArrayList<>();
        list1.add(1);
        list2.add(1);
        alist.add(1);
        list2.add(1);
        alist.add(1);
        finallist.add(list1);
        finallist.add(list2);
        for (int j = 3;j <= numRows;j++) {
            List<Integer> list3 = new ArrayList<>();
            list3.add(1);
            for (int i = 0; i < alist.size() - 1; i++) {
                list3.add(alist.get(i) + alist.get(i + 1));
            }
            list3.add(1);
            finallist.add(list3);
            alist.clear();
            alist.addAll(list3);
        }
        return finallist;
    }
}

In my wrong code,I was declaring the 'list3' outside the outer loop.在我的错误代码中,我在外循环之外声明了“list3”。 Added the 'alist' to my 'finallist' which is the actually my answer.将“alist”添加到我的“finallist”中,这实际上是我的答案。 The elements got copied to 'alist' again from 'list3' after clearing the previous 'alist'.As I had to enter elements for the next row in my 'list3', I was clearing the elements of 'list3' in order to enter elements for next row.清除前一个“alist”后,元素再次从“list3”复制到“alist”。由于我必须在“list3”中输入下一行的元素,因此我正在清除“list3”的元素以便输入下一行的元素。

In my correct code, the only difference is that I declared 'list3' outside the inner loop but inside the outer loop for which I need not to clear 'list3' for next row iteration.It automatically will be cleared as 'list3' is being called/declared outside the inner loop,ieelements are automatically refreshed.在我正确的代码中,唯一的区别是我在内部循环之外声明了“list3”,但在外部循环内部,我不需要为下一行迭代清除“list3”。它会在“list3”被清除时自动被清除在内部循环之外调用/声明,即元素会自动刷新。

I think most probably I am making some logical error in the 'object.clear' part in ""Wrong Code"" for which the elements are not inserted in the final one but getting cleared.我想很可能我在“错误代码”中的“object.clear”部分犯了一些逻辑错误,其中元素没有插入最后一个但被清除。

Can anyone clear my doubts?谁能解开我的疑惑?

The problem in the wrong code is, that every list3 instance you add in the outer loop to the alist is getting cleared.错误代码中的问题是,您在外循环中添加到 alist 的每个 list3 实例都被清除。

You see, by adding a List x to another List y, you are not copying the values from the list x to the other list y, but you are giving the reference to list x to the list y.您会看到,通过将列表 x 添加到另一个列表 y,您并没有将列表 x 中的值复制到另一个列表 y,而是将列表 x 的引用提供给列表 y。

And by clearing list x, after you give list x to list y, you clear the all values.通过清除列表 x,在将列表 x 提供给列表 y 之后,您将清除所有值。

Thats why after the first two list (which you do not clear), all lists added in the loop to the final list, are references to empty lists, hence:这就是为什么在前两个列表(您不清楚)之后,在循环中添加到最终列表的所有列表都是对空列表的引用,因此:

[[1], [1, 1], [], [], []] [[1], [1, 1], [], [], []]

You can see the final list like so:你可以看到这样的最终列表:

finnalList[l1, l2, l3, l4, l5] finnalList[l1, l2, l3, l4, l5]

l1 -> List[[1]] l1 -> 列表[[1]]

l2 -> List[[1, 1]] l2 -> 列表[[1, 1]]

l3 -> List[[]] l3 -> 列表[[]]

l4 -> List[[]] l4 -> 列表[[]]

l5 -> List[[]] l5 -> 列表[[]]

You are not copying the values inside the list, but giving references to these list (that you clear in the wrong code) to the final list.您不是在复制列表中的值,而是将这些列表的引用(您在错误的代码中清除)提供给最终列表。

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

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