[Algorithm -- Dynamic programming] How Many Ways to Decode This Message?
For example we have
'a' -> 1
'b' -> 2
..
'z' -> 26
By given "12", we can decode the string to give result "ab" or 'L', 2 ways to decode, your function should return 2 as an answer.
Now asking by given "1246", what should be the return number;
The thinking process is somehow like this:
by given "1" -> we got 'a'
by given "" -> we got ""
by given "12345" -> 'a' + decode('2345') or 'L' + decode('345'), therefore number of ways to decode "12345"is the same of decode(2345)+decode(345).
Somehow we can see that this is a recursion task, therefore we can use Dynamice Programming + memo way to solve the problem.
const data = ""; function num_ways(data) {
// k : count from last to beginning
function helper(data, k, memo) {
if (k === ) {
// if k equals 0, mean only one single digital number left
// means there must be one char
return ;
} if (data === "") {
// if data equals empty, then return 1
return ;
} if (memo[k] != null) {
return memo[k];
} const start = data.length - k;
if (data[start] === "") {
// if sth start as 0, then no char
return ;
} let result = helper(data, k - , memo); if (k >= && parseInt(data.slice(start, start + ), ) <= ) {
result += helper(data, k - , memo);
} memo[k] = result; return result;
} let memo = [];
return helper(data, data.length, memo);
} const res = num_ways(data);
console.log(res); //
[Algorithm -- Dynamic programming] How Many Ways to Decode This Message?的更多相关文章
- [Algorithm -- Dynamic Programming] Recursive Staircase Problem
For example there is a staricase N = 3 | ---| |---| | |---| | ---| ...
- Algorithm: dynamic programming
1. Longest Increasing Subsequence (LIS) problem unsorted array, calculate out the maximum length of ...
- [Algorithm] Dynamic programming: Find Sets Of Numbers That Add Up To 16
For a given array, we try to find set of pair which sums up as the given target number. For example, ...
- leetcode@ [91] Decode Ways (Dynamic Programming)
https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...
- 以计算斐波那契数列为例说说动态规划算法(Dynamic Programming Algorithm Overlapping subproblems Optimal substructure Memoization Tabulation)
动态规划(Dynamic Programming)是求解决策过程(decision process)最优化的数学方法.它的名字和动态没有关系,是Richard Bellman为了唬人而取的. 动态规划 ...
- Dynamic Programming: From novice to advanced
作者:Dumitru 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=dynProg An impo ...
- [Optimization] Dynamic programming
“就是迭代,被众人说得这么玄乎" “之所以归为优化,是因为动态规划本质是一个systemetic bruce force" “因为systemetic,所以比穷举好了许多,就认为是 ...
- About Dynamic Programming
Main Point: Dynamic Programming = Divide + Remember + Guess 1. Divide the key is to find the subprob ...
- Dynamic Programming
We began our study of algorithmic techniques with greedy algorithms, which in some sense form the mo ...
随机推荐
- Java反射机制demo(二)—通过Class实例化任意类的对象
Java反射机制demo(二)—通过Class实例化任意类的对象 上一章节中,实例化了Class类对象的实例,这个部分的demo展示了如何使用Class对象的实例去获得其他类的对象的实例. 任意一个类 ...
- python-爬虫技能升级记录
====== python-爬虫技能升级记录 ====== ===== (一)感知爬虫及爬取流程 =====<code>从简单存取一个页面到 爬取到大量的定量数据,对技术要求更高,以百度百 ...
- Xml配置文件属性的说明
Xml配置文件属性的说明: <bean id="TheAction" ⑴ class="net.xiaxin.spring.qs.UpperAction" ...
- appengine 云计算。 部署web网络。
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha appengine 可以 不用手动启动像服务器. 在eclipse中 这两个sdk 配好 ...
- web前端 -- onkeydown、onkeypress、onkeyup、onblur、onchange、oninput、onpropertychange的区别
FROM:http://www.cnblogs.com/svage/archive/2011/11/15/2249954.html onkeydown:按下任何键(字母.数字.系统.tab等)都能触发 ...
- HDU 5641 King's Phone 模拟
King's Phone 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5641 Description In a military parade, ...
- MySQL遇到的一个卡库问题及对update的学习
近日遇到个卡库的问题,环境是MySQL5.5.12,报错信息如下 ) and was aborted. There is a chan ce that your master is inconsist ...
- 使用OData快速构建REST服务
OData是微软支持的一种查询标准,它的第四版使用了REST规范,看起来简洁多了.它的最大的特点是可以在客户端自行配制查询条件,使用它构建REST服务时再也不用担心查询的扩展性问题了. 如下是几个简单 ...
- 【转载】利用Matlab制作钟表
静态时钟 hObject=figure; set(hObject,'NumberTitle','off'); set(hObject,'MenuBar','none'); set(hObject,'v ...
- photoshop:制作sprite拼贴图片
目标: 将 合并为一张图片: 第一步:制作动作,便于批处理和重复使用 首先随便新建空白文档录制动作,alt+F9 创建新动作->1.打开一个小图2.图像->模式->RGB(避免有的图 ...