-
Notifications
You must be signed in to change notification settings - Fork 0
/
91. 解码方法.cc
64 lines (63 loc) · 1.72 KB
/
91. 解码方法.cc
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Solution {
public:
int numDecodings(string s)
{
//dp[i] : 字符串s前i个字符存在的编码数
//s[i]=='0'
// s[i-1] == '0' || s[i-1] > 2 非法
// 否则,dp[i] = dp[i-2]
//s[i-1] == '0' 此时s[i]!='0',肯定i-1合法
// dp[i] = dp[i-1]
//s[i-1] == '1'
// dp[i] = dp[i-1] + dp[i-2]
//s[i-1] == '2'
// s[i] <= 6
// dp[i] = dp[i-1] + dp[i-2]
// 否则,dp[i] = dp[i-1]
//否则,dp[i] = dp[i-1]
if(s[0] == '0') return 0;
vector<int> dp(s.size()+1);
dp[0] = dp[1] = 1;
for(int i=1; i<s.size(); ++i)
{
if(s[i] == '0')
{
if(s[i-1] == '0' || s[i-1] > '2') return 0; //非法
else dp[i+1] = dp[i-1];
}
else if(s[i-1] == '0') dp[i+1] = dp[i];
else if(s[i-1] == '1') dp[i+1] = dp[i] + dp[i-1];
else if(s[i-1] == '2')
{
if(s[i] <= '6') dp[i+1] = dp[i] + dp[i-1];
else dp[i+1] = dp[i];
}
else dp[i+1] = dp[i];
}
return dp[s.size()];
}
};
/* 优化 */
class Solution {
public:
int numDecodings(string s)
{
if(s[0] == '0') return 0;
int cur=1, prev=1, tmp;
for(int i=1; i<s.size(); ++i)
{
tmp = cur;
if(s[i] == '0')
{
if(s[i-1] == '0' || s[i-1] > '2') return 0; //非法
else cur = prev;
}
else if(s[i-1] == '1' || (s[i-1] == '2' && s[i] <= '6'))
{
cur = cur + prev;
}
prev = tmp;
}
return cur;
}
};