A message containing letters from A-Z
is being encoded to numbers using the following mapping:
'A' -> 1 'B' -> 2 ... 'Z' -> 26
Given an encoded message containing digits, determine the total number of ways to decode it.
For example,
Given encoded message
"12"
, it could be decoded as "AB"
(1 2) or "L"
(12).The number of ways decoding
"12"
is 2.题目地址: https://oj.leetcode.com/problems/decode-ways/ 这道题真是写的想死啊!!!!主要是因为送来解码的字符中有可能有0,而单独的0是不能解码的就要返回0;关于这个问题要添加很多很多要判断的条件。。不过也许是我自己的算法没有考虑全面。wrong answer无数多次之后终于ac了...
代码:
【leetcode:Decode Ways】
class Solution {
public:
int numDecodings(string s) {
int len=s.size();
if(len==0) return 0;
if(s[0]=='0') return 0;
vector ways(len,0);
ways[0]=1;
for(int i=1;
i=1) return true;
else return false;
}
};
推荐阅读
- leetcode做题笔记|leetcode:Subsets
- leetcode做题笔记|leetcode:
- leetcode做题笔记|leetcode:Restore IP Addresses
- leetcode:Reverse Linked List II