leetcode - Decode Ways
2013-03-28 12:12 张汉生 阅读(214) 评论(0) 编辑 收藏 举报题目描述:点击此处
1 class Solution { 2 public: 3 int numDecodings(string s) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int len = s.length(); 7 if (len ==0 || (len>=1&&s.at(0)=='0')) 8 return 0; 9 else if (len==1) 10 return 1; 11 int lastTwo = 1; int lastOne = 1; 12 int cur; 13 for (int i=1; i<len; i++){ 14 cur = 0; 15 if (s.at(i)!='0') 16 cur = lastOne; 17 if (s.at(i-1)=='1' || (s.at(i-1)=='2'&&s.at(i)<='6')) 18 cur += lastTwo; 19 lastTwo = lastOne; 20 lastOne = cur; 21 } 22 return cur; 23 } 24 };