最坏情况下的时间复杂度计算如下(假设字符串最大长度为 n):newStr = '' # will be done once so 1 time.for chr in string: # is iterating on the input with max length of n so n times. if chr.isspace(): # will be checked once it is in the loop so 1 time per each iteration. newStr = newStr + ' ' # also once per iteration if the if condition is satisfied else: # will be chehcked once per iteration newStr += str(ord(chr)) # if else is satisfiedreturn newStr # will be done 1 time.我们将假设常数时间是 c 所以:Time complexity = 1 + n(c*c + c*c) + 1 = 2+Cn => O(n)