Pascal'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?

用递归的方法实现,C++代码如下:

#include<iostream>
#include<vector>
using namespace std;

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        if(rowIndex==0)
            return {1};
        vector<int> vec1(rowIndex);
        vector<int> vec2(rowIndex+1);
        vec1=getRow(rowIndex-1);
        vec2[0]=1;
        for(int i=1;i<rowIndex;i++)
        {
            vec2[i]=vec1[i-1]+vec1[i];
        }
        vec2[rowIndex]=1;
        return vec2;
    }
};

int main()
{
    Solution s;
    int n;
    cout<<"input n is :";
    cin>>n;
    vector<int> vec=s.getRow(n);
    for(auto v:vec)
        cout<<v<<" ";
    cout<<endl;
}

运行结果如下:

时间: 2024-07-31 15:35:36

Pascal&#39;s Triangle II的相关文章

[LeetCode]119.Pascal&amp;#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? 思路 无 代码 /**------------------------------------ * 日期:2015-02-06 * 作者:S

[LeetCode]118.Pascal&amp;#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] ] 思路 模拟 代码 /**------------------------------------ * 日期:2015-02-06 * 作者:SJF0115 * 题目: 118.Pascal's

【LeetCode从零单排】No118 Pascal&amp;#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] ] 代码 public class Solution { public List<List<Integer>> generate(int numRows) { List<List

Pascal&amp;#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] ] c++代码如下: #include<iostream> #include<vector> using namespace std; class Solution { public: ve

基于Java实现杨辉三角 LeetCode Pascal&#039;s Triangle_java

 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] ] 这道题比较简单, 杨辉三角, 可以用这一列的元素等于它头顶两元素的和来求. 数学扎实的人会看出, 其实每一列都是数学里的排列组合, 第4行, 可以用 C30 =

leetcode难度及面试频率

转载自:LeetCode Question Difficulty Distribution               1 Two Sum 2 5 array sort         set Two Pointers 2 Add Two Numbers 3 4 linked list Two Pointers           Math 3 Longest Substring Without Repeating Characters 3 2 string Two Pointers      

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

List中的set方法和add方法

[LeetCode]–119. Pascal's Triangle II 在做这个题的时候,我发现了一个list初始化的问题.就是set必须是new出来的具体list初始化之后才能使用,不然就会报错.下面就研究一下set和add. package yanning; import java.util.LinkedList; import java.util.List; public class TestListSet { public static void main(String[] args)

LeetCode总结【转】

转自:http://blog.csdn.net/lanxu_yy/article/details/17848219 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近完成了www.leetcode.com的online judge中151道算法题目.除各个题目有特殊巧妙的解法以外,大部分题目都是经典的算法或者数据结构,因此做了如下小结,具体的解题思路可以搜索我的博客:LeetCode题解 题目 算法 数据结构 注意事项 Clone Graph BFS 哈希表 Word Ladder II