I am trying to generate the pascal's triangle for a lines. For 5 it gives me only the 5th row five times. Dont know why?
public class Solution {
public 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.clear();
internal.add(1);
for(int j = 0 ; j < i ; j++ )
{
internal.add(internal.get(j) * (i - j)/(j + 1));
}
result.add(internal);
}
return result;
}
}
Expected:- [1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]
Getting:- [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]
Need reason why this is happening