|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- A message containing letters from A-Z is being encoded to numbers using the following mapping:
- 'A' -> 1
- 'B' -> 2
- ...
- 'Z' -> 26
- Given a non-empty string containing only digits, determine the total number of ways to decode it.
- Example 1:
- Input: "12"
- Output: 2
- Explanation: It could be decoded as "AB" (1 2) or "L" (12).
- Example 2:
- Input: "226"
- Output: 3
- Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
复制代码
dp solution
- class Solution {
- public:
- int numDecodings(string s) {
-
- if(s.length() == 0 || s[0] == '0') return 0;
- vector <int> dp(s.length()+1,0);
- dp[0] = 1;
- dp[1] = 1;
- int max1 = 0;
- for(int i = 2; i <= s.length(); i++){
-
- if(s[i-1] >= '1' && s[i-1] < '27'){
- dp[i] += dp[i-1];
- }
-
- if(s[i-2] == '1' || (s[i-2] == '2' && s[i-1] >= '0' && s[i-1] <= '6')){
- dp[i] += dp[i-2];
- }
-
-
- }
- return dp[s.length()];
- }
- };
复制代码 |
|