YEZAELP

LeetCode: Decode Ways

Nov 3rd, 2021 (edited)
585
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2. public:
  3.     int dp[110];
  4.     int numDecodings(string s) {
  5.         int len = s.size();
  6.         if(s[0] == '0') return 0;
  7.        
  8.         dp[0] = dp[1] = 1;
  9.         for(int i=2;i<=len;i++){
  10.             if(s[i-1] != '0')  
  11.                 dp[i] += dp[i-1];
  12.             if(i > 1 and ((s[i-2] == '1') or (s[i-2] == '2' and s[i-1] <= '6')))
  13.                 dp[i] += dp[i-2];
  14.         }
  15.         return dp[len];
  16.     }
  17. };
RAW Paste Data Copied