Decode Ways解题笔记

题目:以A-》1, B-》2.。。。。的顺序解码一个字符串序列, 因为存在解码歧义所以例如 12-》L或者存在12-》AB,所以题目是给定一个数字字符串,可以给出多少组合结果:

个人分析:

1.在给定字符串中0,1,2的存在会产生歧义,所以要处理好0,1,2的问题

2.如果两个数字的值组合小于26则会出现多种解法

3. 排列组合学的不好,然后看答案了。。。。

题目分类:

字符串,动态规划

有关动态规划的学习:

1. 将问题分解为若干个子问题,类似题目之中,将字符串s的每一位都分解为一个子问题然后计算他的解法

2.一旦某个给定子问题的解已经算出,则将其记忆化存储,以便下次需要同一个子问题解之时直接查表, 建立一个数组,将每一位的结果存入数组,如果下一位的结果小于26的,期结果是前两位的加和。

3. 如果两位的加和小于26则是result[i+1]+result[i+2],如果小于26则是result[i+1];

4. 动态规划的思想在于将问题分解:

例如在这一个题目中就是将字符串分解为若干个字符,然后逐一进行分析,最后迭代得出结果。

代码:

public int numDecodings(String s) {
     int n = s.length();
if(s==null||n==0) return 0;
int[] result = new int[n+1];
result[n] =1;
result[n-1] = s.charAt(n-1)!=‘0‘?1:0;
for(int i= n-2; i>=0 ; i--){
if(s.charAt(i)==‘0‘)
continue;
else
result[i]=(Integer.parseInt(s.substring(i,i+2))<=26)?result[i+1]+result[i+2]:result[i+1];
}
return result[0];
}

时间: 2024-10-25 20:47:35

Decode Ways解题笔记的相关文章

LeetCode:Decode Ways 解题报告

Decode WaysA message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total number of ways to decode it. For example,Given e

[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

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

[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]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】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

44. Decode Ways &amp;&amp; Gray Code

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, G