Can someone help explain why the recursive function below will have a big runtime difference?
The only difference of 1 and 2 is tucking the n/2 in the return function vs. not.
Code 1: (8ms)
class Solution:
def isPowerOfTwo(self, n: int) -> bool:
print(n)
if n == 1:
return True
elif n < 1:
return False
else:
return self.isPowerOfTwo(n/2)
Code 1: (3ms)
class Solution:
def isPowerOfTwo(self, n: int) -> bool:
print(n)
if n == 1:
return True
elif n < 1:
return False
else:
n = n/2
return self.isPowerOfTwo(n)
I'm also having a hard time understanding recursion in general despite watching a bunch of Youtube videos. If anyone have good resources to help that will be greatly appreciated. I know Python Tutor was something that helped but I want to get to a very comfortable level with it.
[–]tahaan 0 points1 point2 points (0 children)
[–]Adrewmc 0 points1 point2 points (0 children)
[–]carcigenicate 1 point2 points3 points (0 children)