【发布时间】:2013-07-31 05:52:47
【问题描述】:
给定一个映射:
A: 1
B: 2
C: 3
...
...
...
Z: 26
找出可以表示数字的所有可能方式。例如。对于输入:“121”,我们可以将其表示为:
ABA [using: 1 2 1]
LA [using: 12 1]
AU [using: 1 21]
我尝试考虑使用某种动态编程方法,但我不确定如何继续。我在一次技术面试中被问到这个问题。
这是我能想到的解决方案,如果看起来不错,请告诉我:
A[i]: Total number of ways to represent the sub-array number[0..i-1] using the integer to alphabet mapping.
解决方案[我错过了什么吗?]:
A[0] = 1 // there is only 1 way to represent the subarray consisting of only 1 number
for(i = 1:A.size):
A[i] = A[i-1]
if(input[i-1]*10 + input[i] < 26):
A[i] += 1
end
end
print A[A.size-1]
【问题讨论】:
-
您是否必须打印所有可能的组合或可能的组合数量?
-
如果输入是101呢?它是否分为 10,1 和 1,01?
-
@Fallen: 可能组合的数量
-
杰森,你是对的。
标签: algorithm dynamic combinations