Pascal's triangle: Difference between revisions

Content added Content deleted
(→‎Summing from Previous Rows: initial "last" is never used)
Line 341: Line 341:
if(rows < 0) return;
if(rows < 0) return;
//save the last row here
//save the last row here
LinkedList<Integer> last = null;
LinkedList<Integer> last = new LinkedList<Integer>();
for(int i= 0;i <= rows;++i){
for(int i= 0;i <= rows;++i){
//work on the next row
//work on the next row
LinkedList<Integer> thisRow= new LinkedList<Integer>();
LinkedList<Integer> thisRow= new LinkedList<Integer>();
for(int j= 0;j < i;++j){//loop the number of elements in this row
for(int j= 0;j <= i;++j){//loop the number of elements in this row
if(j == last.size() || j == 0){//if we're on the ends
if(j == last.size() || j == 0){//if we're on the ends
thisRow.add(1);
thisRow.add(1);
Line 352: Line 352:
}
}
}
}
thisRow.add(1);
last= thisRow;//save this row
last= thisRow;//save this row
System.out.println(thisRow);
System.out.println(thisRow);