[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 ...
随机推荐
- ZOJ 3953 Intervals
线段树,排序. 按照$R$从小到大排序之后逐个检查,如果$L$,$R$最大值不超过$2$,那么就把这个区间放进去,区间$+1$,否则不能放进去. #include<bits/stdc++.h&g ...
- Bzoj2120/洛谷P1903 数颜色(莫队)
题面 Bzoj 洛谷 题解 考虑对操作离线后分块处理询问操作(莫队算法),将询问操作按照编号分块后左端点第一关键字,右端点第二关键字排序(分块大小为\(n^{\frac 23}\)),对于每一个询问操 ...
- 初探 Spring Boot
近些年Spring Boot都特别火,一直都想来学习学习,奈何近期公司项目繁忙,一直都没有时间来学习,今天终于是休息一天,于是来一睹 SpringBoot 的风采. 一.什么是Spring Boot ...
- 51nod1981 如何愉快地与STL玩耍
先摆官方题解吧......... ....................有什么好讲的呢....... 注意一些地方常数优化一下.......然后......$bitset$怎么暴力怎么来吧..... ...
- POJ 2728 JZYZOJ 1636 分数规划 最小生成树 二分 prim
http://172.20.6.3/Problem_Show.asp?id=1636 复习了prim,分数规划大概就是把一个求最小值或最大值的分式移项变成一个可二分求解的式子. #include< ...
- HOJ 2645 WNim sg函数 博弈论
http://blog.csdn.net/y1196645376/article/details/52165245 这道题我没有写,因为我就算翻译了我也找不到数据范围,也分不清数据变量的命名,而且ho ...
- 2017-2018-1 JAVA实验站 冲刺 day04
2017-2018-1 JAVA实验站 冲刺 day04 各个成员今日完成的任务 小组成员 今日工作 完成进度 张韵琪 写博客.进行工作总结 100% 齐力锋 找背景音乐 100% 张浩林 游戏操作说 ...
- PAT甲级1012. The Best Rank
PAT甲级1012. The Best Rank 题意: 为了评估我们第一年的CS专业学生的表现,我们只考虑他们的三个课程的成绩:C - C编程语言,M - 数学(微积分或线性代数)和E - 英语.同 ...
- jsonp和jsonpcallback的使用
1. jsonp.jsonpCallback jsonp跨域时可以自定义的两个参数 2. jsonp: 回掉函数名的参数名,默认callback,服务端通过它来获取到回掉函数名 3. jsonpCa ...
- struts2类型转化器详解(带例子)
Struts2有两种类型转化器: 一种局部,一种全局. 如何实现: 第一步:定义转化器 第二部:注册转化器 下面做一个局部类型转化器的实例. 我们在上面一片日志说过有个变量date类型的.只有我们输入 ...