classSolution { public: intnumDecodings(string s){ if (s.empty()) return0; int n = s.length(); int f[n + 1] = {1}; for (int i = 1; i <= n; ++i) { if (s[i - 1] != '0') { f[i] += f[i - 1]; } if (i > 1) { auto ret = stoi(s.substr(i - 2, 2)); if (10 <= ret && ret <= 26) { f[i] += f[i - 2]; } } } return f[n]; } };
classSolution { public: intnumDecodings(string s){ if (s.empty()) return0; int n = s.length(); int f0 = 1, f1 = 1; for (int i = 1; i <= n; ++i) { int f2 = 0; if (s[i - 1] != '0') { f2 += f1; } if (i > 1) { auto x = stoi(s.substr(i - 2, 2)); if (10 <= x && x <= 26) { f2 += f0; } } f0 = f1; f1 = f2; } return f1; } };
classSolution { public: intnumDecodings(string s){ if (s.empty()) return0; int n = s.length(); int f0 = 1, f1 = 1; for (int i = 0; i < n; ++i) { int f2 = 0; if (s[i] != '0') { f2 += f1; } if (i > 0) { auto x = stoi(s.substr(i - 1, 2)); if (10 <= x && x <= 26) { f2 += f0; } } f0 = f1; f1 = f2; } return f1; } };