题解 | #把数字翻译成字符串#

把数字翻译成字符串

http://www.nowcoder.com/practice/046a55e6cd274cffb88fc32dba695668

class Solution {
public:
    /**
     * 解码
     * @param nums string字符串 数字串
     * @return int整型
     */
    int solve(string nums) {
        // write code here
        int n = nums.size();
        vector<int> f(n + 1, 0);
        // 初始化
        if(nums[0] > '0')
            f[0] = f[1] = 1;
        for(int i = 1; i < n; i++) {
            auto c = nums[i];
            auto pre = nums[i-1];
            if(pre == '0' && c == '0')
                continue;
            f[i + 1] = f[i]; // 当前的字符翻译需要一位数
            if(pre > '0' && pre <= '2') { 
                if(pre == '2' && c > '6' || c == '0') // 二位数字符和一位数字符情况相同
                    continue;
                f[i + 1] += f[i - 1];
            }
        }
        return f[n];
    }
};
全部评论

相关推荐

点赞 评论 收藏
分享
06-08 22:25
门头沟学院 Java
从零开始的转码生活:这hr不会打开手机不分青红皂白给所有人群发这句话,过一会再给所有人再发一遍,这肯定会有重复的,不管,再过一会再发一遍
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务