Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
climbing-stairs.py 1019 Bytes
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2019-01-01 17:11 +08:00 . Update climbing-stairs.py
# Time: O(logn)
# Space: O(1)
import itertools
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
def matrix_expo(A, K):
result = [[int(i==j) for j in xrange(len(A))] \
for i in xrange(len(A))]
while K:
if K % 2:
result = matrix_mult(result, A)
A = matrix_mult(A, A)
K /= 2
return result
def matrix_mult(A, B):
ZB = zip(*B)
return [[sum(a*b for a, b in itertools.izip(row, col)) \
for col in ZB] for row in A]
T = [[1, 1],
[1, 0]]
return matrix_expo(T, n)[0][0]
# Time: O(n)
# Space: O(1)
class Solution2(object):
"""
:type n: int
:rtype: int
"""
def climbStairs(self, n):
prev, current = 0, 1
for i in xrange(n):
prev, current = current, prev + current,
return current
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助