Skip to content

Commit 1c3118c

Browse files
authoredDec 25, 2023
Create 91 Decode ways.cpp
1 parent eb856df commit 1c3118c

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
 

‎91 Decode ways.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
int numDecodings(string s) {
4+
int n=s.length();
5+
if(n==0 || s[0]=='0') {
6+
return 0;
7+
}
8+
9+
int prev1=1,prev2=1;
10+
11+
for(int i=1;i<n;++i)
12+
{
13+
int current=0;
14+
if(s[i]!='0') {
15+
current+=prev2;
16+
}
17+
int digits=stoi(s.substr(i-1,2));
18+
if(digits>=10 && digits<=26)
19+
{
20+
current+=prev1;
21+
}
22+
23+
prev1=prev2;
24+
prev2=current;
25+
}
26+
27+
return prev2;
28+
}
29+
};

0 commit comments

Comments
 (0)