首页 > 编程语言 > 详细

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

时间:2016-08-11 11:27:04      阅读:351      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!