class Solution {
public:
bool isdigit(char c)
{
return c >= '0'&&c <= '9';
}
int numDecodings(string s) {
int dp[1000] = { 1 };//dp[i]表示前i个字符的方
if (s.length() == 0 || s[0] == '0')
return 0;
dp[0] = 1;
int temp=0;
dp[1] = 1;
string substr = "";
//dp[1] = s[1] == '0' ? dp[0] : dp[0] + 1;
for (int i = 2; i <= s.length(); i++)
{
substr = s.substr(i-2, 2);
temp = (substr[0] - '0') * 10 + (substr[1] - '0');
if (temp>=10 && temp <= 26)
{
dp[i] += dp[i - 2];
}
substr = s.substr(i - 1, 1);
temp = (substr[0] - '0');
if (temp >= 1 && temp <= 9)
dp[i] += dp[i - 1];
}
return dp[s.length()];
}
};
LeetCode decode ways
最新推荐文章于 2019-05-23 09:48:25 发布