lc118.java 919 字节
Newer Older
L
liu13 已提交
1
package code;
L
liu13 已提交
2 3 4 5 6 7 8 9 10
/*
 * 118. Pascal's Triangle
 * 题意:上层元素相邻的和,两边补1,得到下层元素
 * 难度:Easy
 * 分类:Array
 * 思路:迭代计算
 * Tips:
 */
import java.util.ArrayList;
L
liu13 已提交
11 12 13 14
import java.util.List;

public class lc118 {
    public List<List<Integer>> generate(int numRows) {
L
liu13 已提交
15 16 17 18 19 20 21
        List<List<Integer>> res = new ArrayList<>();
        if(numRows==0) return res;
        List<Integer> ls = new ArrayList();
        ls.add(1);  //第一层1个1
        res.add(ls);
        numRows--;
        if(numRows==0) return res;
L
liu13 已提交
22
        while(numRows>0){
L
liu13 已提交
23 24 25 26 27 28 29 30 31
            ArrayList<Integer> temp = new ArrayList();
            temp.add(1);
            for (int i = 0; i < ls.size()-1 ; i++) {
                temp.add(ls.get(i)+ls.get(i+1));
            }
            temp.add(1);
            res.add(temp);
            ls = temp;
            numRows--;
L
liu13 已提交
32
        }
L
liu13 已提交
33
        return res;
L
liu13 已提交
34 35
    }
}