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

杨辉三角(一)

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

import java.util.*;


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

全部评论

相关推荐

ResourceUt...:楼主有自己的垃圾箱,公司也有自己的人才库
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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