当前位置:网站首页>[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()];
}
};
边栏推荐
- 【云享读书会第13期】音频文件的封装格式和编码格式
- Catalog component design and custom extended catalog implementation in spark3
- Binder初始化过程
- Using Prometheus to monitor spark tasks
- C语言:函数栈帧
- [tensorboard] oserror: [errno 22] invalid argument processing
- Using Lombok results in the absence of parent class attributes in the printed toString
- Inter thread wait and wake-up mechanism, singleton mode, blocking queue, timer
- C语言:自定义类型
- Spark 3.0 testing and use
猜你喜欢

C language: function stack frame

【剑指offer】面试题45:把数组排成最小的数

【剑指offer】面试题49:丑数

C language: minesweeping games
![[daily question 1] 558. Intersection of quadtrees](/img/96/16ec3031161a2efdb4ac69b882a681.png)
[daily question 1] 558. Intersection of quadtrees

Analysis of spark task scheduling exceptions

What format is this data returned from the background

C language: Sanzi game

实体类(VO,DO,DTO)的划分

Implement custom spark optimization rules
随机推荐
It is said that the US government will issue sales licenses to Huawei to some US enterprises!
Under the ban, the Countermeasures of security giants Haikang and Dahua!
[系统编程] 进程,线程问题总结
NPM install error unable to access
C语言:三子棋游戏
The method of exchanging two numbers in C language
[sword finger offer] interview question 39: numbers that appear more than half of the time in the array
【云享读书会第13期】FFmpeg 查看媒体信息和处理音视频文件的常用方法
Go language slow start - Basic built-in types
[sword finger offer] interview question 54: the k-largest node of the binary search tree
网络设备硬核技术内幕 路由器篇 22
【剑指offer】面试题50:第一个只出现一次的字符——哈希表查找
[daily question 1] 558. Intersection of quadtrees
Multimap case
使用Lombok导致打印的tostring中缺少父类的属性
Use deconstruction to exchange the values of two variables
CAS compares the knowledge exchanged, ABA problems, and the process of lock upgrading
The difference between synchronized and reentrantlock
Text batch replacement function
Network device hard core technology insider router Chapter 22