LeetCode 118. Pascal's Triangle && LeetCode 119. Pascal's Triangle II

118. Pascal's Triangle

题目

Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.


In Pascal's triangle, each number is the sum of the two numbers directly above it.

Example:

Input: 5
Output:
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

这道题,就,brute force了。真的没有什么好的算法,就是要看怎样才能把代码写得简洁优雅又易懂了。我刚开始写出的代码很笨,而且直接先push了1进去导致没考虑到numRows=0的情况第一次提交的时候还WA了真丢人。这个是我非常笨重的代码,时间4ms,89.22%,空间8.8M,42.74%。

/*
 * @lc app=leetcode id=118 lang=cpp
 *
 * [118] Pascal's Triangle

 */
class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        vector<vector<int>> result;
        if (numRows == 0) {
            return result;
        }
        vector<int> first(1, 1);
        result.push_back(first);
        for (int i = 1; i < numRows; i++) {
            vector<int> temp;
            temp.push_back(1);
            for (int j = 1; j < i; j++) {
                temp.push_back(result[i - 1][j] + result[i - 1][j - 1]);
            }
            temp.push_back(1);
            result.push_back(temp);
        }
        return result;
    }
};

后来看了一下discussion,总结出了两点改进的方法:

1. 提前把temp的所有元素置为1,vector<int> temp(i + 1, 1),这样就不用前后push 1进去,只用操作中间的数,而且不必担心numsRow=0的情况,写起来真的很简洁优雅!代码如下:

/*
 * @lc app=leetcode id=118 lang=cpp
 *
 * [118] Pascal's Triangle

 */
class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        vector<vector<int>> result;
        for (int i = 0; i < numRows; i++) {
            vector<int> row(i + 1, 1);
            for (int j = 1; j < i; j++) {
                row[j] = result[i - 1][j] + result[i - 1][j - 1];
            }
            result.push_back(row);
        }
        return result;
    }
};


2. 每次copy上一次的数组,在这个数组的基础之上衍生出两个数组,分别是在前面加个0,和在后面加个0,两个数组相加得到最终结果。这种方法的构思非常巧妙,感觉也不算是brute force严格按照定义来了,并且应该是O(k)的空间复杂度,比前两个O(n)的优秀。

/*
 * @lc app=leetcode id=118 lang=cpp
 *
 * [118] Pascal's Triangle

 */
class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        vector<vector<int>> result;
        vector<int> row;
        for (int i = 0; i < numRows; i++) {
            row.push_back(1);
            for (int j = i - 1; j > 0; j--) {
                row[j] = row[j] + row[j - 1];
            }
            result.push_back(row);
        }
        return result;
    }
};



119. Pascal's Triangle II

题目:

Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.

Note that the row index starts from 0.


In Pascal's triangle, each number is the sum of the two numbers directly above it.

Example:

Input: 3
Output: [1,3,3,1]

Follow up:

Could you optimize your algorithm to use only O(k) extra space?


这个实际上和上一道题是一样的,只是只输出最后一行而不是输出全部,因此上面的两种方法均适用,只需要去掉最外层的双层vector就行。在看discussion的时候也看到了一种数学的做法,说其实帕斯卡三角形就是组合数,第n行第k个的数字就是C(n, k),同时也是(a+b)^n的展开项的系数,不看都没意识到。然后写这个组合数的求法也写了我老半天,太菜了QAQ 它的变量设置的非常巧妙,而且为了防止int溢出以及除法产生小数,采用的是long的数据类型。它的时间复杂度是 O(n),空间复杂度是O(k)。代码如下,4ms,90.34%,8.7M,26.13%:

/*
 * @lc app=leetcode id=119 lang=cpp
 *
 * [119] Pascal's Triangle II
 */
class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<int> row(rowIndex + 1, 1);
        int middle = rowIndex / 2;
        int j = 1;
        long c = 1;

        for (int i = rowIndex; i > middle; i--) {
            c *= i;  // 组合数分母中的k*(k-1)*...
            c /= j;  // 组合数分子中的1*2*...
            row[j] = (int)c;  // 从前往后
            row[i - 1] = (int)c;  // 从后往前
            j++;
        }
        return row;
    }
};

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值