feat: added alternative solution and TLE solution to decode-ways problem, wrote the tutorial file and added some more manual test cases
This commit is contained in:
45
decode-ways/src/alternative_ac.cpp
Normal file
45
decode-ways/src/alternative_ac.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
typedef long long ll;
|
||||
using namespace std;
|
||||
|
||||
ll numDecodings(string s)
|
||||
{
|
||||
if (s[0] == '0')
|
||||
return 0;
|
||||
|
||||
int N = s.size();
|
||||
ll dp1 = 1, dp2 = 1;
|
||||
for (int i = 2; i <= N; i++)
|
||||
{
|
||||
ll next = 0;
|
||||
char current = s[i - 1], last = s[i - 2];
|
||||
string aux = "";
|
||||
aux += last;
|
||||
aux += current;
|
||||
if (current != '0')
|
||||
{
|
||||
next = dp2;
|
||||
}
|
||||
|
||||
int code = stoi(aux);
|
||||
if (10 <= code && code <= 26)
|
||||
{
|
||||
next += dp1;
|
||||
}
|
||||
swap(dp1, dp2);
|
||||
swap(dp2, next);
|
||||
}
|
||||
|
||||
return dp2;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
string code;
|
||||
cin >> code;
|
||||
cout << numDecodings(code) << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user