我正在尝试为线条生成pascal的三角形。对于5,它只给我第五行五次。不知道为什么?
<td>
预期: - [1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]
得到: - [1,4,6,4,1],[1,4,6,4,1],[1,4,6,4,1],[1,4,6,4,1] [1,4,6,4,1]
需要为什么会发生这种情况
答案 0 :(得分:0)
import java.util.ArrayList;
public class Solution {
public static void main(String args[]){
System.out.print(generate(3));
}
public static ArrayList<ArrayList<Integer>> generate(int a) {
ArrayList<Integer> internal = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
for(int i = 0 ; i < a ; i++)
{
internal = new ArrayList<Integer>(); //this is the keypoint.
internal.add(1);
for(int j = 0 ; j < i ; j++ )
{
internal.add(internal.get(j) * (i - j)/(j + 1));
}
result.add(internal);
}
return result;
}
}
输出
<块引用>[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]