Python題解
class Solution:
def GetTranslationCount(self, number):
def helper(number):
length = len(number)
counts = [0] * length
count = 0
for i in range(length-1, -1, -1):
count = 0
if i < length - 1:
count = counts[i+1]
else:
count = 1
if i < length - 1:
digit1 = int(number[i]) - int('0')
digit2 = int(number[i+1]) - int('0')
converted = digit1 * 10 + digit2
if converted >= 10 and converted <= 25:
if i < length - 2:
count += counts[i+2]
else:
count += 1
counts[i] = count
count = counts[0]
return count
numberInString = str(number)
return helper(numberInString)
考點
- 考查分析問題的能力。能夠分析出遞歸的表達式是解決這個問題的前提;
- 考查對遞歸及時間效率的理解。如果只是能夠把遞歸分析轉換爲遞歸代碼,不一定能夠通過這道題的面試,而是能夠用基於循環的代碼來避免不必要的重複計算。