leetcode119~Pascal's Triangle II

    xiaoxiao2021-03-25  130

    Given an index k, return the kth row of the Pascal’s triangle. For example, given k = 3, Return [1,3,3,1].

    public class PascalTriangleII { //这里每到一行,都会有对应的数1了,所以可以直接从前向后遍历 public List<Integer> getRow2(int rowIndex) { List<Integer> res = new ArrayList<>(); for(int i=0;i<=rowIndex;i++) { res.add(0, 1); for(int j=1;j<i;j++) { res.set(j, res.get(j)+res.get(j+1)); } } return res; } //从后向前遍历 public List<Integer> getRow(int rowNums) { List<Integer> res = new ArrayList<Integer>(); //初始化第一个元素为1 res.add(1); for(int i=0;i<rowNums;i++) { for(int j=res.size()-2;j>=0;j--) { res.set(j+1, res.get(j)+res.get(j+1)); } //每行最后一个数为1 res.add(1); } return res; } }
    转载请注明原文地址: https://ju.6miu.com/read-12683.html

    最新回复(0)