int numDecodings(string s)
{
if (s.length() == 0 || s[0] == '0')
{
return 0;
}
else if (s.length() == 1)
{
return 1;
}
int numWithoutLastTwo = 1, numWithoutLastOne = 1, num = 0;
for (int i = 1; i < s.length(); i++)
{
num = 0;
if (s[i] != '0')
{
num += numWithoutLastOne;
}
if (s[i-1] == '1' || ((s[i-1] == '2') && (s[i] <= '6')))
{
num += numWithoutLastTwo;
}
numWithoutLastTwo = numWithoutLastOne;
numWithoutLastOne = num;
}
return num;
}
[LeetCode] Decode Ways
最新推荐文章于 2019-05-23 09:48:25 发布