Skip to content

Commit a519273

Browse files
committed
3
1 parent 26321d0 commit a519273

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

DecodeWays/DecodeWays.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int numDecodings(string s) {
4+
// Start typing your C/C++ solution below
5+
// DO NOT write int main() function
6+
7+
if (s.empty()) return 0;
8+
9+
vector<int> dp(s.size() + 1, 0);
10+
dp[0] = 1;
11+
for (int i = 1; i <= s.size(); i++) {
12+
if (s[i-1] != '0')
13+
dp[i] = dp[i-1];
14+
if (i > 1 && s[i-2] != '0') {
15+
int x = s[i-2] - '0';
16+
x = 10 * x + s[i-1] - '0';
17+
if (0 < x && x <= 26)
18+
dp[i] += dp[i-2];
19+
}
20+
}
21+
return dp[s.size()];
22+
}
23+
};

0 commit comments

Comments
 (0)