leetcode------Pascal's Triangle

标题: Pascal's Triangle
通过率: 30.7%
难度: 简单

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]

]

中国人一般都叫这个杨辉三角,国外人叫pascal,这个题道理都是明白的。。就是实现起来的问题了。。道理比较简单,一动手全部瞎,这种题超级适合出笔试题,即简单,实用,还不能被鄙视着吐槽说笔试太难,具体细节看代码就行了:
 1 public class Solution {

 2     public List<List<Integer>> generate(int numRows) {

 3         List<List<Integer>> result=new ArrayList<List<Integer>>();

 4         for(int i=0;i<numRows;i++){

 5             List<Integer> tmp=new ArrayList<Integer>();

 6             tmp.add(1);

 7             if(i>0){

 8                 for(int j=0;j<result.get(i-1).size()-1;j++)

 9                     tmp.add(result.get(i-1).get(j)+result.get(i-1).get(j+1));

10                     tmp.add(1);

11             }

12             result.add(tmp);

13         }

14         return result;

15     }

16 }

 

你可能感兴趣的:(LeetCode)