我正在做这个leetcode 问题,要求反转一个整数。我编写了这个方法,使用分而治之的递归技术将 int 转换为 string 并反转。我想知道这个时间复杂度是O(log n)吗?(n是字符串中的字符数)。
def __reverse_recur__(self, num: str):
N = len(num)
# if we are reduced to only a single char, return it
if N == 1:
return num
else:
n = int(N / 2) # index to split string from middle
# concatenate the recursion result as follows:
# recurse on the right-part of the string to place it as the left half of the concatenation
left_half = self.__reverse_recur__(num[n:])
# …Run Code Online (Sandbox Code Playgroud)