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.

解题思路:

使用动态规划的思想。

(一)

初始想法可以建立一个数组,数组的每一位保存对应字符串的每一位可能出现的解码方式,例如:

字符串321321312;其对应的截止到每一位可能存在的解码方式种类为:

3 - 1种

32 - 1种

321 - 2种

3213 - 3种

32132 - 5种

....

所以得到解码方式数组为:

原字符串S:    3  2  1  3  2  1  3  1  2

解码种类数组A:  1  1  2  3  5  8 16 16 32

发现:

(1)如果当前字符可以和上一个字符组成二元字符,例如'1'和'2'组成'12',那么A[i] = A[i-1] + A[i-2];

  很好理解,等于将'1' '2'分开的解码种类与将'12'合起来的解码种类之和;

(2)如果当前字符不能和上一个字符组成二元字符,那么A[i] = A[i-1];

(二)

因此,进一步的思路是,不需要建立一个数组,只需要保存i-2和i-1的解码种类数就可以了,分别用r1和r2表示;

如果当前字符可以组成二元字符,那么当前字符的次数 = r1 + r2;

如果不能,当前字符次数 = r1;

(三)

进一步发现,只用r1和r2两个变量就够了

(1)每次遇到可以组成二元字符的字符时,就更新r1,使r2等于旧的r1:

  r1= r1 + r2;

  r2 = r1;

(2)当不能组成二元字符时,r1不变,更新r2为r1;

(3)最终返回r1;

注意:

如果当前字符是'0',如果将'0'单独算,则不能产生任何解码数,即解码数为0;

代码:

 int numDecodings(string s) {
if (!s.size() || s.front() == '') return ;
int r1 = , r2 = ; for (int i = ; i < s.size(); i++) {
if (s[i] == '') r1 = ; if (s[i - ] == '' || s[i - ] == '' && s[i] <= '') {
r1 = r2 + r1;
r2 = r1 - r2;
} else {
r2 = r1;
}
} return r1;
}

【Leetcode】【Medium】Decode Ways的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【leetcode刷题笔记】Decode Ways

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  6. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  7. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  8. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

  9. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

  10. 【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum

    [Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...

随机推荐

  1. Ubuntu 安装 fcitx 输入法

    fcitx 和 ibus一样都是输入法框架.下面介绍ubuntu下安装fcitx输入法. 1.先卸载系统中的输入法 2.安装. 增加ppa源:sudo add-apt-repository ppa:f ...

  2. MySql生日闰月处理

    1. 科普下润年: ①.非整百年能被4整除的为闰年.(如2004年就是闰年,2100年不是闰年) ②.整百年能被400整除的是闰年.(如2000年是闰年,1900年不是闰年)   2. 例: 例如:当 ...

  3. 几种不同的获取url地址的方法

    通过如下的几种方法,您就可以获取访问者访问您的网站的来路,请根据需要选择适合您的方法. 一.C#代码一 string url = Request["referer"]; Respo ...

  4. 上传文件时$_FILES为空的解决方法

    上传视频的时候打印$_FILES为空,小的文件就没问题,后来发现是因为传的文件太大, 出现这个问题的原因主要有两个:表单原因或者php设置原因: 1,表单类型: 上传文件的表单编码类型必须设置成 en ...

  5. @Configuration 和 @Bean

    1. @Bean: 1.1 定义 从定义可以看出,@Bean只能用于注解方法和注解的定义. @Target({ElementType.METHOD, ElementType.ANNOTATION_TY ...

  6. Solr Zookeeper ACL权限配置

    首先注意:在配置ACL的时候,请关闭solr运行实例!!否则可能对集群造成不可恢复的损坏 开始: 1.修改solr.xml,在solrCloud节点添加,告诉solr要使用的provider: < ...

  7. centos 下添加epel源

    来源于http://www.centoscn.com/CentOS/config/2014/0920/3793.html,收录备用 0.安装yum优先级插件 yum install yum-prior ...

  8. Linux下yum升级安装PHP 5.5

    我的系统是Centos 6.5 ,安装lnmp是直接yum安装的,php版本为5.4的,当安装了最新的phpMyAdmin(4.5.1)数据库管理软件后发现不支持php5.4使用,所以只好升级下php ...

  9. Log4j基本用法----日志级别

    基本使用方法: Log4j由三个重要的组件构成:日志信息的优先级,日志信息的输出目的地,日志信息的输出格式.日志信息的优先级从高到低有ERROR.WARN.INFO.DEBUG,分别用来指定这条日志信 ...

  10. freeCodeCamp:Return Largest Numbers in Arrays

    右边大数组中包含了4个小数组,分别找到每个小数组中的最大值,然后把它们串联起来,形成一个新数组. 提示:你可以用for循环来迭代数组,并通过arr[i]的方式来访问数组的每个元素. /*思路 for循 ...