Question: https://oj.leetcode.com/problems/pascals-triangle-ii/
Question Name: Pascal’s Triangle II
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | class Solution: # @return a list of integers def getRow(self, rowIndex): ''' Ref: http://en.m.wikipedia.org/wiki/Pascal%27s_triangle Convert this question into: how to compute the consecutive binomial coefficients. ''' if rowIndex == 0: return [1] if rowIndex == 1: return [1, 1] result = [1] # The final result is symmetrical. We could only compute the # the left part, and then append the reversed array of left # part to the end. # COmpute the left part of the symmetrical result nextDivisor = 1 nextMultiplier = rowIndex for _ in xrange(rowIndex // 2): nextVal = (result[-1] * nextMultiplier) / nextDivisor result.append(nextVal) nextDivisor += 1 nextMultiplier -= 1 # Append the reversed array of left part if rowIndex % 2 == 1: result.extend(result[::-1]) else: result.extend(result[-2::-1]) return result |