算法小练——杨辉三角-II


title: 算法小练——杨辉三角 II
categories:

  • Algorithms
    tags:
  • easy
    abbrlink: 292596528
    date: 2019-11-14 18:39:08

杨辉三角 II

描述

给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。

在杨辉三角中,每个数是它左上方和右上方的数的和。

示例:

输入: 3
输出: [1,3,3,1]

进阶:

你可以优化你的算法到 O(k) 空间复杂度吗?

代码

class Solution {
    public List<Integer> getRow(int rowIndex) {
       
        List<Integer> ans = new ArrayList<>();
        if(rowIndex==0){
            ans.add(1);
            return ans;
        }
        if(rowIndex>=1){
            int time =rowIndex;
            List<Integer> alist =getRow(--rowIndex);
            List<Integer> blist = new ArrayList<>();
            blist.add(1);
            for (int i = 1; i < time; i++) {
                blist.add(alist.get(i-1)+alist.get(i));
            }
            blist.add(1);
            return blist;
        }else {
            return ans;
        }
    }
}

笔记

递归算法,实现的

全部评论

相关推荐

05-09 14:45
门头沟学院 Java
点赞 评论 收藏
分享
frutiger:逆天,我家就安阳的,这hr咋能说3k的,你送外卖不比这工资高得多?还说大厂来的6k,打发叫花子的呢?这hr是怎么做到说昧良心的话的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务