Title Description
We all know that Fibonacci number, and now asked to enter an integer n, you export item n Fibonacci Fibonacci number sequence.
run timeout: Your program failed to run at the end of the specified time, check to see if there is wrong or cycle through the large complex algorithms.
case was 0.00% by
Obviously, when implemented using a recursive method, time and space complexity of the complex is too big. Since the function calls itself, but a function call is time and space consumption: every function call, you need the memory stack space allocated to save the parameters, return address and temporary variables, and to the stack pressed into data and pop-up data needs time.
n<=39
method 1:
cycle.
-
# -*- coding:utf-8 -*-
-
class Solution:
-
def Fibonacci(self, n):
-
# write code here
-
if n <=
2:
-
return [
0,
1,
1][n]
-
first, second =
1,
1
-
while n >
2:
-
first, second = second, first + second
-
n -=
1
-
return second
Running time: 25ms
Take up memory: 5728k
Method 2:
Recursion.
-
# -*- coding:utf-8 -*-
-
class Solution:
-
def Fibonacci(self, n):
-
# write code here
-
if n ==
0:
-
return
0
-
Elif
N ==
1
:
-
return
1
-
else:
-
return self.Fibonacci(n -
1) + self.Fibonacci(n -
2)
Fail
Your code has been saved
run timeout: Your program failed to run at the end of the specified time, check to see if there is wrong or cycle through the large complex algorithms.
case was 0.00% by
Obviously, when implemented using a recursive method, time and space complexity of the complex is too big. Since the function calls itself, but a function call is time and space consumption: every function call, you need the memory stack space allocated to save the parameters, return address and temporary variables, and to the stack pressed into data and pop-up data needs time.
Title Description
We all know that Fibonacci number, and now asked to enter an integer n, you export item n Fibonacci Fibonacci number sequence.
run timeout: Your program failed to run at the end of the specified time, check to see if there is wrong or cycle through the large complex algorithms.
case was 0.00% by
Obviously, when implemented using a recursive method, time and space complexity of the complex is too big. Since the function calls itself, but a function call is time and space consumption: every function call, you need the memory stack space allocated to save the parameters, return address and temporary variables, and to the stack pressed into data and pop-up data needs time.
n<=39
method 1:
cycle.
-
# -*- coding:utf-8 -*-
-
class Solution:
-
def Fibonacci(self, n):
-
# write code here
-
if n <=
2:
-
return [
0,
1,
1][n]
-
first, second =
1,
1
-
while n >
2:
-
first, second = second, first + second
-
n -=
1
-
return second
Running time: 25ms
Take up memory: 5728k
Method 2:
Recursion.
-
# -*- coding:utf-8 -*-
-
class Solution:
-
def Fibonacci(self, n):
-
# write code here
-
if n ==
0:
-
return
0
-
Elif
N ==
1
:
-
return
1
-
else:
-
return self.Fibonacci(n -
1) + self.Fibonacci(n -
2)
Fail
Your code has been saved
run timeout: Your program failed to run at the end of the specified time, check to see if there is wrong or cycle through the large complex algorithms.
case was 0.00% by
Obviously, when implemented using a recursive method, time and space complexity of the complex is too big. Since the function calls itself, but a function call is time and space consumption: every function call, you need the memory stack space allocated to save the parameters, return address and temporary variables, and to the stack pressed into data and pop-up data needs time.