【LeetCode OJ】Pascal's Triangle

Posted on 2014-05-13 08:52  卢泽尔  阅读(142)  评论(0)    收藏  举报

Prolbem Link:

http://oj.leetcode.com/problems/pascals-triangle/

Just a nest-for-loop...

class Solution:
    # @return a list of lists of integers
    def generate(self, numRows):
        # Initialize the triangle
        res = []
        for i in xrange(numRows):
            res.append([1] * (i+1))
        # Compute the triangle row by row
        for row in xrange(1, numRows):
            for i in xrange(1, row):
                res[row][i] = res[row-1][i] + res[row-1][i-1]
        # Return
        return res