-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecode_Ways.cpp
More file actions
31 lines (30 loc) · 789 Bytes
/
Copy pathDecode_Ways.cpp
File metadata and controls
31 lines (30 loc) · 789 Bytes
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
# number : 91
class Solution {
public:
int numDecodings(string s) {
if (s[0] == '0')
return 0;
int length = s.size();
int *result = new int[length+1];
result[0] = 1;
result[1] = 1;
for (int i = 1; i < length; i++)
{
if (s[i] == '0')
{
if (s[i-1] == '1' || s[i-1] == '2')
result[i+1] = result[i-1];
else
return 0;
}
else
{
result[i+1] = result[i];
int temp = (s[i-1] - '0') * 10 + (s[i] - '0');
if (temp >= 10 && temp <= 26)
result[i+1] += result[i-1];
}
}
return result[length];
}
};