Pascal's Triangle I,II

题目来自于Leetcode

https://leetcode.com/problems/pascals-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]
]
class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        vector<vector<int> >res;
        for(int i=0;i<numRows;i++)
        {
            vector<int>vec(i+1,1);
            if(i>1)
                for(int j=1;j<i;j++)
                    vec[j]=res[i-1][j-1]+res[i-1][j];
            res.push_back(vec);
            vector<int>().swap(vec);
        }
        return res;
    }
};

Pascal‘s Triangle II

Total Accepted: 42320 Total
Submissions: 143760

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?

此处有内存要求虽然采用第一种方法可以ac但是明显不符合要求

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<vector<int> >res;
        for(int i=0;i<rowIndex+1;i++)
        {
            vector<int>vec(i+1,1);
            if(i>1)
                for(int j=1;j<i;j++)
                    vec[j]=res[i-1][j-1]+res[i-1][j];
            res.push_back(vec);
            vector<int>().swap(vec);
        }
        return res[rowIndex];

    }
};

我们必须重新设计算法。

第一想到的就是pascal三角形的系数会等于N行i列的值等于

( r

     n )

但是

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<int>res(rowIndex+1,1);
        if(rowIndex<2)
        return res;
       long long nth=1;
         for(int i=1;i<rowIndex+1;i++)
            nth*=i;
         long long rth=1,n_rth=nth;
        for(int i=1;i<rowIndex;i++)
        {

            n_rth/=(rowIndex-i+1);
            res[i]=nth/rth/n_rth;
                        rth*=(i+1);
        }
        return res;
    }
};

用来存储二项式系数的值很容易在rowIndex=24时候就报错了

最后一种也就是正确的方法是利用分配的空间来计算的具体给出了k=5的具体描述

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<int>res(rowIndex+1,1);
        if(rowIndex<2)
        return res;
        int t1,t2;
   for(int i=2;i<=rowIndex;i++)
    {
        t1=res[0];
        t2=res[1];
        for(int j=1;j<i+1;j++)
        {
            res[j]=t1+t2;
            t1=t2;
            t2=res[j+1];
        }
        res[i]=1;
    }
        return res;
    }
};

My Submissions

Question
 Solution

Pascal's Triangle I,II

时间: 2024-08-28 06:01:38

Pascal's Triangle I,II的相关文章

Pascal&amp;#39;s Triangle I,II

题目来自于Leetcode https://leetcode.com/problems/pascals-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] ] class Solution { public: vector<vecto

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开心刷题五十一天——118. Pascal&#39;s Triangle 接触跳转表概念,不知用处 lamda逗号导致表达式加法奇怪不理解119. Pascal&#39;s Triangle II

118. Pascal's Triangle Easy 87984FavoriteShare 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],

Java [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]. 解题思路: 每次在上一个list前面插入1,然后后面的每两个间相加赋值给前一个数. 代码描述: public class Solution { public List<Integer> getRow(int rowIndex) { List<Integer> r

[leedcode 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]. public class Solution { public List<Integer> getRow(int rowIndex) { //由于需要O(k)空间,因此需要借助两个数组保存中间值,并交换两个数组,注意交换的方法! List<Integer> list=new

leetcode 【 Pascal&#39;s Triangle II 】python 实现

题目: 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? 代码:oj测试通过 Runtime: 48 ms 1 class Solution: 2 # @return a list of intege

LeetCode119 Pascal&#39;s Triangle II

Given an index k, return the kth row of the Pascal's triangle. (Easy) For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 分析: 上一题的followup,注意只能开O(k)的空间,实际上就是利用滚动数组的解法,每一行只与其上一行有关. 代码: 1 clas

【Pascal&#39;s Triangle II 】cpp

题目: 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? 代码: class Solution { public: vector<int> getRow(int rowIndex) { vector

LeetCode: Pascal&#39;s Triangle II 解题报告

Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question SolutionGiven 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 us