leetcode || 118、Pascal's Triangle

problem:

Given numRows, generate the first numRows of Pascal‘s triangle.

For example, given numRows = 5,

Return

[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

Hide Tags

Array

题意:帕斯卡三角形,又名杨辉三角形,是多项式(a+b)^n的系数

thinking:

(1)杨辉三角形,从第三行开始,排除第一个和最后一个1外,其值是上一行上面的两个元素值之和。参考:维基百科-杨辉三角形

(2)从第三行开始,每一行的元素由上一行的元素决定

code:

class Solution {
private:
    vector<vector<int> > ret;
public:
    vector<vector<int> > generate(int numRows) {
        vector<int> row1(1,1);
        vector<int> row2(2,1);
        ret.clear();
        if(numRows==0)
            return ret;
        if(numRows==1)
        {
            ret.push_back(row1);
            return ret;
        }
        if(numRows==2)
        {
            ret.push_back(row1);
            ret.push_back(row2);
            return ret;
        }
        ret.push_back(row1);
        ret.push_back(row2);
        for(int i=3;i<=numRows;i++)
        {
            vector<int> tmp;
            vector<int> pre=ret[i-2];
            tmp.push_back(1);
            for(int j=0;j<pre.size()-1;j++)
                tmp.push_back(pre[j]+pre[j+1]);
            tmp.push_back(1);
            ret.push_back(tmp);
        }
        return ret;
    }
};

leetcode || 118、Pascal's Triangle

时间: 2024-11-05 22:56:56

leetcode || 118、Pascal's Triangle的相关文章

leetcode || 119、Pascal&#39;s Triangle II

problem: 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? Hide Tags Array 题意:输出杨辉三角形的第K层 即:第K+1行 thinking: 题目要求使用O(K)的额外空间

leetcode 生成杨辉三角形, 118 119 Pascal&#39;s Triangle 1,2

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 解决方案: vector<vector<int>> generate(int numRows) { vector<vector<int>> res = {};

(leetcode题解)Pascal&#39;s Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

LeetCode OJ:Pascal&#39;s Triangle(帕斯卡三角)

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 帕斯卡三角,很简单的问题,见代码: 1 class Solution { 2 public: 3 vector<vector<int>> generate(int numRows) {

118. 119. Pascal&#39;s Triangle -- 杨辉三角形

118. Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] class Solution { public: vector<vector<int>> generate(int numRows) { vector<vector<

【LeetCode】119 - Pascal&#39;s Triangle 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? Solution: 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex){

LeetCode 119:Pascal&#39;s Triangle II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. 题目要求计算杨辉三角某一行的元素,这个也是二项式系数的计算问题. class Solution { public: vector<int> getRow(int rowIndex) { vector<int> result; vector<int> tmp;

LeetCode OJ 119. Pascal&#39;s Triangle 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? [思路] 我们为了满足空间复杂度的要求,我们新建两个ArrayList,一个负责存储上一个Pascal行的结果,一个根据上一个Pascal行得出当前P

LeetCode OJ:Pascal&#39;s Triangle 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? 思路: 因为每一层的数都是对称的,所以只需要计算每一层的前半部分的值,后半部拷贝就可以了.res[i] = res`[i - 1] + res