题解 | #把数字翻译成字符串#
把数字翻译成字符串
http://www.nowcoder.com/practice/046a55e6cd274cffb88fc32dba695668
#
# 解码
# @param nums string字符串 数字串
# @return int整型
#
class Solution:
def solve(self , nums ):
# write code here
retArr = [0]*len(nums)
lenNums = len(nums)
for idx in range(lenNums-1,-1, -1):
if idx == lenNums -1:
if nums[idx] == '0':
retArr[idx] = 0
else:
retArr[idx] = 1
else:
retArr[idx] = retArr[idx+1]
if idx < lenNums -1:
if nums[idx] == '0':
retArr[idx] = 0
continue
item2 = int(nums[idx:idx+2])
if item2 >= 10 and item2 <=26:
if idx < lenNums-2:
retArr[idx] += retArr[idx+2]
else:
retArr[idx] += 1
# print(retArr)
return retArr[0]
# 解码
# @param nums string字符串 数字串
# @return int整型
#
class Solution:
def solve(self , nums ):
# write code here
retArr = [0]*len(nums)
lenNums = len(nums)
for idx in range(lenNums-1,-1, -1):
if idx == lenNums -1:
if nums[idx] == '0':
retArr[idx] = 0
else:
retArr[idx] = 1
else:
retArr[idx] = retArr[idx+1]
if idx < lenNums -1:
if nums[idx] == '0':
retArr[idx] = 0
continue
item2 = int(nums[idx:idx+2])
if item2 >= 10 and item2 <=26:
if idx < lenNums-2:
retArr[idx] += retArr[idx+2]
else:
retArr[idx] += 1
# print(retArr)
return retArr[0]