LeetCode之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.

Climbing Stairs很类似,不过多加一些判断逻辑。

class Solution {
public:
    int numDecodings(string s) {
        if(s.empty() ||s[0]=='0') return 0;
        int cur_2=1,cur_1=1,cur=0;
        for(int i=2;i<=s.size();i++){
            if(s[i-1]!='0') cur+=cur_1;
            if(s[i-2]=='1'||s[i-2]=='2'&& s[i-1]<='6')
                cur+=cur_2;
            cur_2=cur_1;
            cur_1=cur;
            cur=0;
        }
        return cur_1;
    }
};

LeetCode之Decode Ways,布布扣,bubuko.com

时间: 2024-10-28 19:08:59

LeetCode之Decode Ways的相关文章

Leetcode 动态规划 Decode Ways

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Decode Ways Total Accepted: 8689 Total Submissions: 55465 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given a

[LeetCode OJ] 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

Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)

Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给一串包含数字的加密报文,求有多少种解码方式 举个例子,已知报文"12",它可以解码为AB(1 2),也可以是L (12) 所以解码方式有2种. 测试样例 Input: "0" "121212" "1010

[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 encode

leetcode 639 Decode Ways II

首先回顾一下decode ways I 的做法:链接 分情况讨论 if s[i]=='*' 考虑s[i]单独decode,由于s[i]肯定不会为0,因此我们可以放心的dp+=dp1 再考虑s[i-1]和s[i]decode,如果s[i-1]为*,那么**的组合共有15种(11,12,13.....,21,22..26),注意不是9*9=81种,因为我们是s[i-1]和s[i]一起decode,如果是38这样是大于26的 如果s[i-1]不为*,枚举s[i],组合起来小于26即可 if s[i]!

【Leetcode】Decode Ways

题目链接:https://leetcode.com/problems/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 num

[LeetCode] 91. Decode Ways Java

题目: 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 enco

leetcode No91. Decode Ways

Question: 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, Giv

【leetcode】Decode Ways(medium)

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