[LeetCode]91.Decode Ways

简介:

题目

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.

分析

这里写图片描述

代码

/*---------------------------------------
*   日期:2015-06-23
*   作者:SJF0115
*   题目: 91.Decode Ways
*   网址:https://leetcode.com/problems/decode-ways/
*   结果:AC
*   来源:LeetCode
*   博客:
-----------------------------------------*/
#include <iostream>
using namespace std;

class Solution {
public:
    int numDecodings(string s) {
        int size = s.size();
        if(s[0] == '0'){
            return 0;
        }//if
        if(size == 0 || size == 1){
            return size;
        }//if
        int pre = 1,cur = 1,res;
        for(int i = 1;i < size;++i){
            if(isValid(s[i-1],s[i]) && isValid(s[i])){
                res = pre + cur;
            }//if
            else if(!isValid(s[i-1],s[i]) && isValid(s[i])){
                res = cur;
            }//else
            else if(isValid(s[i-1],s[i]) && !isValid(s[i])){
                res = pre;
            }//else
            else{
                return 0;
            }//else
            pre = cur;
            cur = res;
        }//for
        return res;
    }
private:
    bool isValid(char pre,char cur){
        if(pre == '1' || (pre == '2' && cur <= '6')){
            return true;
        }//if
        return false;
    }
    bool isValid(char cur){
        if(cur >= '1' && cur <= '9'){
            return true;
        }//if
        return false;
    }
};

int main(){
    Solution s;
    string str("1202111110");
    cout<<s.numDecodings(str)<<endl;
    return 0;
}

运行时间

这里写图片描述

思路2–超时

/*---------------------------------------
*   日期:2015-06-21
*   作者:SJF0115
*   题目: 91.Decode Ways
*   网址:https://leetcode.com/problems/decode-ways/
*   结果:超时
*   来源:LeetCode
*   博客:
-----------------------------------------*/
#include <iostream>
using namespace std;

class Solution {
public:
    int numDecodings(string s) {
        int size = s.size();
        if(size == 0 || size == 1){
            return size;
        }//if
        int index = -1;
        int count = 0;
        helper(s,index,count,"");
        return count;
    }
private:
    void helper(string &s,int index,int &count,string word){
        int size = s.size();
        if(index == size-1){
            ++count;
            cout<<"word->"<<word<<endl;
            return;
        }//if
        // 步长为1或者2
        int num = 0;
        for(int i = 1;i <= 2;++i){
            if(index + i < size){
                num = num * 10 + s[index+i] - '0';
                if(num <= 26 && num > 0){
                    word += ('A'+num-1);
                    helper(s,index+i,count,word);
                    word.erase(word.size()-1);
                }//if
                else{
                    break;
                }
            }//if
        }//for
    }
};

int main(){
    Solution s;
    string str("1234");
    cout<<s.numDecodings(str)<<endl;
    return 0;
}
目录
相关文章
LeetCode 91. Decode Ways
字母A到Z分别和1到26的数字一一对应,给定一串用字符表示的数字,将数字串从不同位置拆开,每一个数字都对应一个字符,如此构成了一个字母字符串. 从不同的位置拆分数字字符串,可以得到不同的字母字符串,问一共有多少种有效的拆分方式.
47 0
LeetCode 91. Decode Ways
LeetCode 92 Decode Ways
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/51339103 ...
667 0
LeetCode 91 Decode Ways(编码方式)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/51340198 ...
943 0
|
3小时前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
8 0
|
3小时前
|
算法 索引
【刷题】滑动窗口精通 — Leetcode 30. 串联所有单词的子串 | Leetcode 76. 最小覆盖子串
经过这两道题目的书写,相信大家一定深刻认识到了滑动窗口的使用方法!!! 下面请大家继续刷题吧!!!
10 0
|
3小时前
|
算法
【刷题】 leetcode 面试题 08.05.递归乘法
递归算法是一种在计算机科学和数学中广泛应用的解决问题的方法,其基本思想是利用问题的自我相似性,即将一个大问题分解为一个或多个相同或相似的小问题来解决。递归算法的核心在于函数(或过程)能够直接或间接地调用自身来求解问题的不同部分,直到达到基本情况(也称为基础案例或终止条件),这时可以直接得出答案而不必再进行递归调用。
25 4
【刷题】 leetcode 面试题 08.05.递归乘法
|
3小时前
|
存储 算法 安全
【刷题】 leetcode 面试题 01.06 字符串压缩
来看效果: 非常好!!!过啦!!!
25 5
【刷题】 leetcode 面试题 01.06 字符串压缩