【Leetcode】【Medium】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.
解题思路:
使用动态规划的思想。
(一)
初始想法可以建立一个数组,数组的每一位保存对应字符串的每一位可能出现的解码方式,例如:
字符串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的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【leetcode刷题笔记】Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【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 ...
随机推荐
- GRIDVIEW 控件
http://www.cnblogs.com/shanymen/archive/2009/05/22/1486654.html GridView控件是.net里的一个显示数据控件,该控件制作很人性化, ...
- Error : Must specify a primary resource (JAR or python or R file)
spark-submit 报错:must specify resource 取消关注 | 1 ... 我的submit.sh内容: /bin/spark-submit \ --class abc.pa ...
- MySQL_杭州北仓 12.3-12.7需求活动期间累计下单达到3天及以上的客户_20161212
#C025_02杭州北仓 12.3-12.7需求活动期间累计下单达到3天及以上的客户明细 SELECT d.*,CASE WHEN 下单天次>=3 THEN "下单超过3天" ...
- HTTP返回值
100 Continue:初始的请求已经接受,客户应当继续发送请求的其余部分. 101 Switching Protocols:服务器将遵从客户的请求转换到另外一种协议. 200 OK:一切正常,对G ...
- 抽象数据类型ADT
ADT(Abstract Data Type) 类型由什么组成? 一个类型(type)指定两类信息,一个属性集和一个操作集. 假设要定义一个新的数据类型.首先,要提供存储数据的方式,可能是通过设计一个 ...
- DIOCP之DEMO-登陆验证设计(二)
ECHOServer代码(不考虑粘包的处理): unit ufrmMain; interface uses Windows, Messages, SysUtils, Variants, Classes ...
- SVM2---核函数的引入
前边总结了线性SVM,最终转化为一个QP问题来求解.后来又考虑到非线性SVM,如果特征特别特别多的话,直接使用QP的话求解不了,我们经过一系列的转化,把这一问题转化为训练集大小n量级的QP问题. ht ...
- Markdown常用基本语法
现在是我在学习Markdown时做的笔记.学完这些Markdown的基本使用已经不成问题. 1. 标题设置(让字体变大,和word的标题意思一样)在Markdown当中设置标题,有两种方式:第一种:通 ...
- XE3随笔5:Format与转义字符
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...
- freeCodeCamp:Repeat a string repeat a string
重复一个指定的字符串 num次,如果num是一个负数则返回一个空字符串. /*思路 fo循环将字符串重复num次并组成数组 将数组组成新的字符串并返回 */ function repeat(str, ...