LeetCode-Solutions-in-Cpp17

Solutions to high-frequency interview questions of LeetCode in C++17, taking into account both efficiency and comprehensibility.


Project maintained by downdemo Hosted on GitHub Pages — Theme by mattgraham
class Solution {
 public:
  int numDecodings(string s) {
    if (empty(s) || s[0] == '0') {
      return 0;
    }
    vector<int> dp(size(s));
    dp[0] = 1;
    for (int i = 1; i < size(dp); ++i) {
      dp[i] = s[i] == '0' ? 0 : dp[i - 1];
      if (s[i - 1] == '1' || (s[i - 1] == '2' && s[i] <= '6')) {
        dp[i] += i >= 2 ? dp[i - 2] : 1;
      }
    }
    return dp.back();
  }
};