[LeetCode] Decode Ways

来源:互联网 发布:极光推送java服务器端 编辑:程序博客网 时间:2024/06/06 19:21
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;}

0 0
原创粉丝点击