题解 | #杨辉三角(一)#

杨辉三角(一)

https://www.nowcoder.com/practice/4385fa1c390e49f69fcf77ecffee7164

using System;
using System.Collections.Generic;


class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param num int整型
     * @return int整型二维数组
     */
    public List<List<int>> generate (int num) {
        // write code here
        List<List<int>> lsls = new List<List<int>>();
        lsls.Add(new List<int>() {
            1
        });
        for (int i = 1; i < num; i++) {
            List<int> ls = new List<int>();
            lsls.Add(ls);
            for (int j = 0; j < i + 1; j++) {
                int nUB = j - 1 < 0 || j > lsls[i - 1].Count ? 0 : lsls[i - 1][j - 1];
                int nU = j >= lsls[i - 1].Count ? 0 : lsls[i - 1][j];
                ls.Add(nUB + nU);
            }
        }
        return lsls;
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务