程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Sword finger offer python:42 Translate numbers into strings

編輯:Python

subject :

Given a number , We translate it as a string according to the following rules :0 Translate into “a” ,1 Translate into “b”,……,11 Translate into “l”,……,25 Translate into “z”. A number may have more than one translation . Please program a function , Used to calculate how many different translation methods a number has .

 Input : 12258
Output : 5
explain : 12258 Yes 5 Different translations , Namely "bccfi", "bwfi", "bczi", "mcfi" and "mzi"

First , It needs to be clear a-z, The corresponding number range is 0-25, That is, the maximum number combination cannot exceed 25( Less than or equal to 25).

Ideas : recursive .

Code :

class Solution:
def func(self , nums_str):
if len(nums_str) < 2:
return 1
elif len(nums_str) == 2:
if int(nums_str) > 25:
return 1
else:
return 2
return self.func(nums_str[1:]) + self.func(nums_str[2:])


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved