【Leetcode】91. Decode Ways

Yesterday, Bro Luo told me: "Why don‘t you improve your English by writing your blogs in English?" I think it may be a good way and i did so today.

Problem Description:

A message containing letters from A - Z  is being encoded to numbers using the follow 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.

My Solution:

I used dynamic programming to solve it. For each char in string s can be divided in four types.

1.Only can be combined with pre-one

thus dp[i] = dp[i - 2], cause the pre one(i - 1) must combine with i

2.Only can be self alone

thus dp[i] = dp[i - 1]

3. Both can

thus dp[i] = dp[i - 1] + dp[i - 2]

4 Both can NOT:

thus return 0, cause the message can be decode

Of course, return dp[len - 1] if not return before.

My code:

class Solution {
public:
    bool canCombine(char a, char b)
    {
        if(a >= ‘3‘) return false;
        if(a == ‘2‘){
            if(b <= ‘6‘) return true;
            else return false;
        }
        if(a == ‘0‘) return false;
        return true;
    }

    bool canAlone(char a)
    {
        if(a >= ‘1‘ && a <= ‘9‘) return true;
        return false;
    }

    int numDecodings(string s) {
        if(s == "") return 0;
        if(s[0] == ‘0‘) return 0;
        if(s.length() < 2) return 1;
        int dp[50000];
        dp[0] = 1;
        bool canComb = canCombine(s[0],s[1]);
        bool canAlon = canAlone(s[1]);
        if(canComb && canAlon)
            dp[1] = dp[0] + 1;
        else if(!canComb && !canAlon)
            return 0;
        else
            dp[1] = dp[0];
        for(int i = 2; i < s.length(); i ++){
            bool canComb = canCombine(s[i - 1], s[i]);
            bool canAlon = canAlone(s[i]);
            if(canComb && canAlon){
                dp[i] = dp[i - 1] + dp[i - 2];
            }else if(canComb){
                dp[i] = dp[i - 2];
            }else if(canAlon){
                dp[i] = dp[i - 1];
            }else{
                dp[i] = 0;
                return 0;
            }
        }
        return dp[s.length() - 1];
    }
};
时间: 2024-10-07 12:37:25

【Leetcode】91. Decode Ways的相关文章

【LeetCode】091. 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 enco

[leetcode DP]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

【LeetCode】241. Different Ways to Add Parentheses

题目: Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *. Example 1 Input: "2-1-1" ((2-1)-1) = 0 (2-(1-1)) = 2 Outp

[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 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】68、分治递归,medium级别,题目:95、120、91

package y2019.Algorithm.dynamicprogramming.medium; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.dynamicprogramming.medium * @ClassName: NumDecodings * @Author: xiaof * @Description: 91. Decode Ways * A message containing letters from

【LeetCode】动态规划(上篇共75题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [5] Longest Palindromic Substring 给一个字符串,需要返回最长回文子串 解法:dp[i][j] 表示 s[i..j] 是否是回文串,转移方程是 dp[i][j] = 1 (if dp[i+1][j-1] = 1 && s[i] == s[j]),初始化条件是 if (s[i] == s[j] && (i == j

[email&#160;protected] [91] Decode Ways (Dynamic Programming)

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 number of wa

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov