码迷,mamicode.com
首页 > 编程语言 > 详细

(LeetCode)Pascal's Triangle II --- 杨辉三角进阶(滚动数组思想)

时间:2016-08-11 11:28:27      阅读:142      评论: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].

Note:
Could you optimize your algorithm to use only O(k) extra space?

Subscribe to see which companies asked this question


解题分析:

此处有空间的限制,因此不能正常使用迭代算法,

这里推荐滚动数组思想,具体思想在我的上一篇博客里有写。


# -*- coding:utf-8 -*-
__author__ = 'jiuzhang'
class Solution(object):
    def getRow(self, rowIndex):
        if rowIndex < 0:
            return []
        result = [0] * (rowIndex + 1)
        for i in range(rowIndex + 1):
            result[i] = 1
            for j in xrange(i - 1, 0, -1):
                result[j] += result[j - 1]
        return result



(LeetCode)Pascal's Triangle II --- 杨辉三角进阶(滚动数组思想)

标签:

原文地址:http://blog.csdn.net/u012965373/article/details/52180940

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