【leetcode-python-31】面试题10- I. 斐波那契数列
渣渣原始版(62.68%)
如果只对最后结果求余,是37.08%。
先求余后相加再求余,和全部加起来再求余是一样的效果。
class Solution(object):
def fib(self, n):
"""
:type n: int
:rtype: int
"""
import collections
d = collections.deque()
d.append(0)
d.append(1)
d.append(1)
x = 2
if n < 3:
return d[n]
else:
while x<n:
d.popleft()
d.append(sum(d) % 1000000007)
x += 1
return d.pop()
大佬版(99.68%)
动态规划。
作者:jyd
链接:https://leetcode-cn.com/problems/fei-bo-na-qi-shu-lie-lcof/solution/mian-shi-ti-10-i-fei-bo-na-qi-shu-lie-dong-tai-gui/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
class Solution(object):
def fib(self, n):
"""
:type n: int
:rtype: int
"""
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a % 1000000007
新手入坑,多多包涵~~