-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathDecode Ways.cpp
More file actions
45 lines (37 loc) · 805 Bytes
/
Decode Ways.cpp
File metadata and controls
45 lines (37 loc) · 805 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution
{
public:
int numDecodings(string s)
{
if (s.empty())
{
return 0;
}
if (s[0] == '0')
{
return 0;
}
int a = 1;
int b = 1;
for (size_t i = 1; i < s.length(); ++i)
{
int c = 0;
int two_digit_value = (s[i-1] - '0') * 10 + (s[i] - '0');
if (two_digit_value >= 10 && two_digit_value <= 26)
{
c += a;
}
if (s[i] > '0')
{
c += b;
}
a = b;
b = c;
if (b == 0)
{
break;
}
}
return b;
}
};