FTPrep, 119 Pascal's Triangle II

和118一样,只是输出不一样,过程也是类似的。注意comment中提到的细节。 118 和119 还是可以归到 level traversal一类的,和3道树的题目,2到populating pointer的题目,然后这里两道input是array的题目

代码:

class Solution {
    public List<Integer> getRow(int rowIndex) {
        List<Integer> currLevel= new ArrayList<Integer>();
        currLevel.add(1);
        if(rowIndex==0) return currLevel;
        for(int level=1; level<=rowIndex; level++){  // for constructing the new row at "level-th" level
            List<Integer> nextLevel= new ArrayList<Integer>();
            nextLevel.add(1);
            for(int i=1; i<currLevel.size(); i++){  // currLevel从第2个序号到最后一个序号都会被用上!这一点要有概念,做题的时候才熟练。
                nextLevel.add(currLevel.get(i)+currLevel.get(i-1));
            }
            nextLevel.add(1);
            currLevel=nextLevel;
        }
        return currLevel;
    }
}

// 注意了!这里的index是从0开始的,不是1开始的,从例子中的,k 与结果的 size 相差1也可以看出来啊!
// 这和上一题的 numRows 容易混淆。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值