diff options
author | crupest <crupest@outlook.com> | 2021-09-27 20:00:02 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-09-27 20:00:02 +0800 |
commit | 87feba5d6ba7f7f8ff2c67164e2415d24f7eff89 (patch) | |
tree | e1c93e3fb8f79c50699698a991994531107f65e9 /leetcode | |
parent | cf2cea7e417a9d2bc946587d4a6ce48d6d4403d8 (diff) | |
download | solutions-87feba5d6ba7f7f8ff2c67164e2415d24f7eff89.tar.gz solutions-87feba5d6ba7f7f8ff2c67164e2415d24f7eff89.tar.bz2 solutions-87feba5d6ba7f7f8ff2c67164e2415d24f7eff89.zip |
Add leetcode 639.
Diffstat (limited to 'leetcode')
-rw-r--r-- | leetcode/cpp/639.cpp | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/leetcode/cpp/639.cpp b/leetcode/cpp/639.cpp new file mode 100644 index 0000000..ead252e --- /dev/null +++ b/leetcode/cpp/639.cpp @@ -0,0 +1,56 @@ +#include <string> + +using std::string; + +const long long M = 1e9 + 7; + +class Solution { +public: + long long f[100010]{0}; + + int numDecodings(string s) { + f[0] = 1; + if (s.front() == '*') { + f[1] = 9; + } else if (s.front() == '0') { + f[1] = 0; + } else { + f[1] = 1; + } + + for (int i = 2; i <= s.size(); i++) { + char c = s[i - 1], l = s[i - 2]; + if (c == '*') { + f[i] += f[i - 1] * 9; + if (l == '*') { + f[i] += f[i - 2] * 15; + } else if (l == '1') { + f[i] += f[i - 2] * 9; + } else if (l == '2') { + f[i] += f[i - 2] * 6; + } + f[i] %= M; + } else if (c == '0') { + if (l == '1' || l == '2') + f[i] += f[i - 2]; + else if (l == '*') + f[i] += f[i - 2] * 2; + f[i] %= M; + } else { + f[i] += f[i - 1]; + if (l == '*') { + if (c <= '6') + f[i] += f[i - 2] * 2; + else + f[i] += f[i - 2]; + } else if (l == '1') + f[i] += f[i - 2]; + else if (l == '2' && c <= '6') + f[i] += f[i - 2]; + f[i] %= M; + } + } + + return f[s.size()] % M; + } +}; |