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

Leetcode 119 Pascal's Triangle II

时间:2015-06-21 11:48:52      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

Given an index k, return the kth row of the Pascal‘s triangle.

For example, given k = 3,
Return [1,3,3,1].

也可以和 Leetcode 118 Pascal‘s Triangle II 一样构造最后返回最后一行。

var getRow = function(rowIndex) {
    var p = [1]
    for(var i=0;i<rowIndex;i++){
        var temp = p.slice()
        for (var j=1;j<=i;j++){
            p[j] = temp[j-1] + temp[j]
        }
        p.push(1)
    }
    return p
}

这个方法每次在数组两边加0,并应用了 每个等于之前临近两个数之和 的方法在所有数上。

def getRow(self, rowIndex):
    row = [1]
    for i in range(1, rowIndex+1):
        row = list(map(lambda x,y: x+y, [0]+row, row + [0]))
    return row

Leetcode 119 Pascal's Triangle II

标签:

原文地址:http://www.cnblogs.com/lilixu/p/4591747.html

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