返回杨辉三角前 n 层的数字

我的思路后来被证实是比较慢哈,思路很简单,递归地一行一行地往里插。是这样的:

vector<vector<int>> generate(int numRows) {
    if (numRows < 1){
        return vector<vector<int>>();
    }

    if (numRows == 1){
        return vector<vector<int>>{vector<int>{1}};
    }

    auto&& lastLine = generate(numRows - 1);
    vector<int> thisLine;
    for (int i = 0; i != numRows - 1; ++i){
        if (i == 0){
            thisLine.push_back(1);
        }
        else{
            thisLine.push_back(lastLine[numRows - 2][i - 1]
                               + lastLine[numRows - 2][i]);
        }
    }
    thisLine.push_back(1);
    lastLine.push_back(thisLine);
    return lastLine;
}

通过测试用了 4ms, 仍不是最好。出于习惯,我查看了投票数最高的方法,发现他的思路是迭代地填充:

vector<vector<int> > generate(int numRows) {
    vector<vector<int>> r(numRows);

    for (int i = 0; i < numRows; i++) {
        // Set enough places.
        r[i].resize(i + 1);
        // Set the values of the edge.
        r[i][0] = r[i][i] = 1;

        // Evalue values in the middle.
        for (int j = 1; j < i; j++)
            r[i][j] = r[i - 1][j - 1] + r[i - 1][j];
    }

    return r;
}

注释是我加的,所以可能处于中国人看不懂,外国人看不明白的情况。希望谅解,领会精神哈。

时间: 2024-08-08 17:46:22

返回杨辉三角前 n 层的数字的相关文章

Java编程-输出杨辉三角前10行

public class YanghuiTriangle { public static void main(String[] args) { int triangle[][]=new int[10][];// 创建二维数组 // 遍历二维数组的第一层 for (int i = 0; i < triangle.length; i++) { triangle[i]=new int[i+1];// 初始化第二层数组的大小 // 遍历第二层数组 for(int j=0;j<=i;j++){ if(i

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

从第0行开始,输出第k行,传的参数为第几行,所以在方法中先将所传参数加1,然后将最后一行加入集合中返回. 代码如下: public static List<Integer> generateII(int row){ ++row; List<Integer> list = new ArrayList<Integer>(); int[][] arr = new int[row][row]; for(int j = 0;j<row;j++) { for(int k =

POJ 3187 Backward Digit Sums (杨辉三角,穷竭搜索,组合数,DFS)

http://poj.org/problem?id=3187 将一行数按杨辉三角的规则计算为一个数,已知最后那个数和三角形的高度,求最初的那行数. 杨辉三角前10行:                   1                                   1   1                               1   2   1                           1   3   3   1                       1  

Pascal&#39;s Triangle 2 杨辉三角之二

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 杨辉三角想必大家并不陌生,应该最早出现在初高中的数学中,其实就是二项式系数的一种写法. 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1

[LeetCode] 119. Pascal&#39;s Triangle II 杨辉三角 II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 118. Pascal's Triangle 的拓展,给一个索引k,返回杨辉三角的第k行. 解法:题目要求优化到 O(k) 的空间复杂,那么就不能把每

算法练习之杨辉三角,杨辉三角的第 k 行

1. 杨辉三角 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] java class Solution { public List<List<Integer>> generate(int numRows) { List<List<Integer>> rs = n

C语言118. 杨辉三角

给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5输出:[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]] 下面是我的常规解法:没有用到指针,但是力扣上的返回类型是这样的 :int** generate(int numRows, int* returnSize, int** returnColumnSizes) #include <stdio.h>int mai

杨辉三角-list实现(优化)

计算杨辉三角前6行(升级版),list实现     值如下:     [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1],[1,5,10,10,5,1]] 思路:     上一个实现的思路,下一行的数字是基于上一行的数字求得而来,那么则只需要使用两     个内存空间,分别来保存这两行数字.一个保存上一行的数字,另一行则保存求得的下一     行数字.一轮循环后,新生成的下一行变成了旧行,新行清空重新计算并填充. 优化思路:     直接开辟一个足够大的内存空间,在

算法基础_递归_求杨辉三角第m行第n个数字

问题描述: 算法基础_递归_求杨辉三角第m行第n个数字(m,n都从0开始) 解题源代码(这里打印出的是杨辉三角某一层的所有数字,没用大数,所以有上限,这里只写基本逻辑,要符合题意的话,把循环去掉就好): import java.util.Scanner; /** * 求杨辉三角第m层第n个数字 * @author Administrator * */ public class Demo05 { public static int f(int m,int n) { if(n==0)return 1