码迷,mamicode.com
首页 > 其他好文 > 详细

119 Pascal's Triangle II 帕斯卡三角形 II Pascal's Triangle II

时间:2018-04-05 13:27:18      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:三角形   etc   asc   https   ++i   row   highlight   problems   get   

给定一个索引 k,返回帕斯卡三角形(杨辉三角)的第 k 行。
例如,给定 k = 3,
则返回 [1, 3, 3, 1]。
注:
你可以优化你的算法到 O(k) 的空间复杂度吗?
详见:https://leetcode.com/problems/pascals-triangle-ii/description/

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

 

119 Pascal's Triangle II 帕斯卡三角形 II Pascal's Triangle II

标签:三角形   etc   asc   https   ++i   row   highlight   problems   get   

原文地址:https://www.cnblogs.com/xidian2014/p/8721953.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!