Decode Ways -- leetcode

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 类似

第i位之前,decode共有两种途径:

1. 第i-1位能独立decode

2. 第i-2 和 i-1位,两位一起被当作个整体decode

则deocde方法数,为上面两个途径之和。

此代码在leetcode上,实行执行时间为5ms。

class Solution {
public:
    int numDecodings(string s) {
        if (s.empty()) return 0;
        int w2 = 1;
        int w1 = s[0] != '0' ? 1 : 0;
        int w = w1;
        for (int i=2; i<=s.size(); i++) {
            w = (s[i-1] == '0') ? 0 : w1;
            if (s[i-2] == '1' || s[i-2] == '2' && s[i-1] >= '0' && s[i-1] <= '6')
                w += w2;

            w2 = w1;
            w1 = w;
        }
        return w;
    }
};
时间: 2024-10-23 00:53:11

Decode Ways -- leetcode的相关文章

Decode Ways leetcode 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 enc

Decode Ways [leetcode] DP

题目:求将数字字符串s转换成字母有多少种可能的结果? dp(i):s[0...i-1]可能的结果数.我们设si = i-1 分析s[si]: 1. s[si - 1] = '1' || s[si - 1] = '2' && s[si] = '6' s[0...si]可以转换成s[si]对应的字母 + s[0...si-1]对应的字母,也可能是 s[si-1..si]对应的字母 + s[0...si-2]对应的字母 即dp(i) = dp(i -1) + dp(i - 2) 2. s[si]

[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之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] Decode Ways [33]

题目 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: Decode Ways [090]

[题目] 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 en

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刷题笔记】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] Decode Ways(DP)

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