当前位置:网站首页>[sword finger offer] interview question 46: translating numbers into strings - dynamic programming
[sword finger offer] interview question 46: translating numbers into strings - dynamic programming
2022-07-27 15:52:00 【Jocelin47】

Method 1 : Dynamic programming
By establishing the transfer equation f(i) = f(i-1) + g(i,i-1)*f(i-2)
If the first two numbers are 10-25 Between , be g(i,i-1) by 1, Otherwise 0
class Solution {
public:
int translateNum(int num) {
string nums = to_string(num);
vector<int> dp( nums.size() + 1, 0);
dp[0] = 1;
dp[1] = 1;
for ( int i = 2; i <= nums.size(); i++)
{
dp[i] += dp[i - 1];
int temp = ( nums[i - 2] - '0' ) * 10 + ( nums[ i -1 ] - '0');
if( temp >=10 && temp <= 25 )
dp[i] += dp[i - 2];
}
return dp[nums.size()];
}
};
边栏推荐
- Network principle (1) - overview of basic principles
- 【剑指offer】面试题55 - Ⅰ/Ⅱ:二叉树的深度/平衡二叉树
- Division of entity classes (VO, do, dto)
- Multimap case
- JS find the maximum and minimum values in the array (math.max() method)
- Go language learning notes (1)
- The method of exchanging two numbers in C language
- 【剑指offer】面试题46:把数字翻译成字符串——动态规划
- Jump to the specified position when video continues playing
- 线程中死锁的成因及解决方案
猜你喜欢

QT (XIII) qchart drawing line chart

Inter thread wait and wake-up mechanism, singleton mode, blocking queue, timer

Spark Bucket Table Join

C语言:动态内存函数

C language: string function and memory function

初识结构体

The difference between synchronized and reentrantlock

C language: custom type

NPM install error unable to access

Using Lombok results in the absence of parent class attributes in the printed toString
随机推荐
【剑指offer】面试题41:数据流中的中位数——大、小堆实现
使用双星号代替Math.pow()
$router.back(-1)
使用Lombok导致打印的tostring中缺少父类的属性
Push down of spark filter operator on parquet file
Go language slow start - Basic built-in types
Set the position of the prompt box to move with the mouse, and solve the problem of incomplete display of the prompt box
Implement custom spark optimization rules
[Yunxiang book club issue 13] packaging format and coding format of audio files
【剑指offer】面试题49:丑数
线程中死锁的成因及解决方案
C language: Sanzi game
Voice live broadcast system -- a necessary means to improve the security of cloud storage
【剑指offer】面试题52:两个链表的第一个公共节点——栈、哈希表、双指针
Database: use the where statement to retrieve (header song)
JS find the maximum and minimum values in the array (math.max() method)
Analysis of spark task scheduling exceptions
Interview focus - TCP protocol of transport layer
【剑指offer】面试题45:把数组排成最小的数
Spark 3.0 DPP implementation logic