119. Pascal's Triangle II*
2016-08-31 16:27
274 查看
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return
Note:
Could you optimize your algorithm to use only O(k) extra space?
Reference
class Solution(object):
def getRow(self, rowIndex):
"""
:type rowIndex: int
:rtype: List[int]
"""
result = [0]*(rowIndex+1)
result[0]= 1
for i in range(1,rowIndex+1):
for j in range(i,0,-1):
result[j] +=result[j-1]
return result
Note:
It is obvious that we should find the law of numbers in certain row of Pascal's Triangle.
According to materials online, the nth number in mth row is C(m,n-1).However, this method needs a lot of computation.
For example, given k = 3,
Return
[1,3,3,1].
Note:
Could you optimize your algorithm to use only O(k) extra space?
Reference
class Solution(object):
def getRow(self, rowIndex):
"""
:type rowIndex: int
:rtype: List[int]
"""
result = [0]*(rowIndex+1)
result[0]= 1
for i in range(1,rowIndex+1):
for j in range(i,0,-1):
result[j] +=result[j-1]
return result
Note:
It is obvious that we should find the law of numbers in certain row of Pascal's Triangle.
According to materials online, the nth number in mth row is C(m,n-1).However, this method needs a lot of computation.
相关文章推荐
- 119. Pascal's Triangle II
- 119. Pascal's Triangle II
- 119. Pascal's Triangle II(unsolved)
- 【一天一道LeetCode】#119. Pascal's Triangle II
- LeetCode(119) Pascal's Triangle II
- [LeetCode] 119. Pascal's Triangle II
- 【LeetCode】119.Pascal Triangle II
- [leetcode]#119. Pascal's Triangle II
- LeetCode力扣之119. Pascal's Triangle II
- LeetCode119 Pascal's Triangle II
- 119. Pascal's Triangle II
- 119 Pascal's Triangle II
- LeetCode 119. Pascal's Triangle II
- LeetCode 119: Pascal's Triangle II
- 119. Pascal's Triangle II LeetCode
- LeetCode-119:Pascal's Triangle II (返回杨辉三角指定行)
- leetcode-119. Pascal's Triangle II
- 119. Pascal's Triangle II
- 119. Pascal's Triangle II
- leetcode - 119. Pascal's Triangle II