Pascal Triangle

Question:

//Given numRows, generate the first numRows of Pascal's triangle.
//
//For example, given numRows = 5, Return
//
//[
//     [1],
//    [1,1],
//   [1,2,1],
//  [1,3,3,1],
// [1,4,6,4,1]
//]

Answer:

package LeetCode;

import java.util.*;

public class pascalTriangle {

//failed method 
    
//  public static int[][] pascalTriangle(int rows){
//      int[][] pascal = new int[rows][];
//
//      for(int i =0;i> triangle(int rows){
        List> result = new ArrayList>();
        if (rows<=0) return result;
        List pre= new ArrayList();
        pre.add(1);
        result.add(pre);
        for (int i=2;i<=rows;i++){
            List cursor = new ArrayList();
            cursor.add(1);
            for(int j=0;j

Result

1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 

你可能感兴趣的:(Pascal Triangle)