classSolution: defgenerate(self, numRows: int) -> List[List[int]]: pascal = list() L = [1] for t in range(numRows): LL = L[:] pascal.append(LL) L.append(0) L = [L[i - 1] + L[i] for i in range(len(L))] return pascal
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal’s triangle.
Note that the row index starts from 0.
In Pascal’s triangle, each number is the sum of the two numbers directly above it.
Example:
1 2
Input: 3 Output: [1,3,3,1]
Follow up:
Could you optimize your algorithm to use only O(k) extra space?
Solution
1 2 3 4 5 6 7 8
classSolution: defgetRow(self, rowIndex: int) -> List[int]: pascal = list() L = [1] for t in range(rowIndex): L.append(0) L = [L[i - 1] + L[i] for i in range(len(L))] return L
# -*- coding: utf-8 -*- """ Created on Mon Jun 22 19:17:30 2015
@author: Piiska """
mCache = {} def pascalWithDict(n,k): if n==k or k==0 or n==1: return 1 if k==1: return n if mCache.has_key((n,k)): return mCache[(n,k)] else: mCache[n,k] = pascalWithDict(n-1,k-1)+pascalWithDict(n-1,k) return mCache[n,k]
## 获得每行pascal列表 def generatePascal(depth): lines = [] for row in range(depth): line = [] for col in range(row+1): line.append(pascal(row, col)) lines.append(line) return lines
if __name__ =="__main__": high = int(raw_input("pls enter the height of pascal:")) lines = generatePascal(high) for i in range(high): print lines[i]
1 2 3 4 5 6 7 8 9 10 11 12
NUM =input() defprintLine(lineList): lineList = [str(temp) for temp in lineList] print("%s%s" % (" " * (NUM - len(lineList)), " ".join(lineList))) for i in range(NUM): if i < 2: pascal = [1] * (i + 1) else: pascal[1:-1] = [(temp + pascal[j]) for j, temp in enumerate(pascal[1:])] printLine(pascal)
The following code demonstrates how to implement a so-called Pascal’s Triangle.
1 2 3 4 5 6 7 8 9 10 11 12
deftriangles(): L = [1] while1: yield L L.append(0) L = [L[i - 1] + L[i] for i in range(len(L))] n = 0 for t in triangles(): print(t) n = n + 1 if n == 10: break
# def subsetsWithDup(nums): # sub = [[]] # for i in nums: # for j in range(len(sub)): # sub.append(sub[j]+[i]) # import itertools # sub.sort() # return list(sub for sub,_ in itertools.groupby(sub)) # return list(map(list,set(map(tuple, sub)))) # return [list(s) for s in (set([tuple(l) for l in sub]))]
# def subsetsWithDup(nums): # sub = [[]] # for i in nums: # for j in range(len(sub)): # sub.append(sorted(sub[j]+[i])) # sets = set([tuple(l) for l in sub[1:]]) # return sum([k[0] for k in sets]) # # 没顺序要求
# print(subsetsWithDup([3,1,2,4]))
defgenerate(numRows): deftriangles(): L = [1] while1: yield L L.append(0) L = [L[i - 1] + L[i] for i in range(len(L))] n = 0 pascal = list() for t in triangles(): tt = t[:] pascal.append(tt) n = n + 1 if n == numRows: break return pascal
print(generate(6))
1 2 3 4 5 6 7 8 9 10 11
num = int(input('Number of rows: ')) yh = [[]] * num for row in range(len(yh)): yh[row] = [None] * (row + 1) for col in range(len(yh[row])): if col == 0or col == row: yh[row][col] = 1 else: yh[row][col] = yh[row - 1][col] + yh[row - 1][col - 1] print(yh[row][col], end=' ') print()