-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoffer46.java
More file actions
36 lines (35 loc) · 1.3 KB
/
offer46.java
File metadata and controls
36 lines (35 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* [46] 把数字翻译成字符串
*
* 题目: 将给定数字按照如下规则把它翻译为字符串: 0 翻译成 “a”, 1 翻译成 “b”, ... , 25 翻译成 “z”. 返回有多少种不同的翻译方法.
*
* 思路: 动态规划: f(i) 表示 i 位置到数字结尾的数字有多少种翻译方法.
* 状态转移方程: f(i) = f(i - 1) + f(i - 2).
*/
class Solution {
/**
* 时间复杂度: O(n)
* 空间复杂度: O(n)
*/
public int translateNum(int num) {
String str = String.valueOf(num);
int len = str.length();
int[] dp = new int[len + 1];
// dynamic programing's base case.
dp[len] = 1;
for (int i = len - 1; i >= 0; i--) {
// state transfer equation: f(i) = f(i - 1) + f(i - 2).
dp[i] = dp[i + 1];
// if current number start with '0',
// it can't take two place in the front to translate to string.
if (str.charAt(i) != '0') {
// if current number take two place is bigger than 25,
// it also can't translate to string.
if (i + 2 <= len && Integer.valueOf(str.substring(i, i + 2)) <= 25) {
dp[i] += dp[i + 2];
}
}
}
return dp[0];
}
}