当前位置:网站首页>[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()];
}
};
边栏推荐
- 为应对RISC-V挑战?Arm CPU引入自定义指令功能!
- C语言中交换两数的方法
- Implement custom spark optimization rules
- 【剑指offer】面试题49:丑数
- [Yunxiang book club issue 13] packaging format and coding format of audio files
- Using Lombok results in the absence of parent class attributes in the printed toString
- 【剑指offer】面试题42:连续子数组的最大和——附0x80000000与INT_MIN
- js使用for in和for of来简化普通for循环
- Use deconstruction to exchange the values of two variables
- /Dev/loop1 takes up 100% of the problem
猜你喜欢

C语言:自定义类型

What format is this data returned from the background

UDP 的报文结构和注意事项

Alibaba's latest summary 2022 big factory interview real questions + comprehensive coverage of core knowledge points + detailed answers

Complexity analysis

数组名是首元素地址吗?

Implement custom spark optimization rules

C语言:三子棋游戏

【剑指offer】面试题41:数据流中的中位数——大、小堆实现

【剑指offer】面试题50:第一个只出现一次的字符——哈希表查找
随机推荐
为应对RISC-V挑战?Arm CPU引入自定义指令功能!
Breaking through soft and hard barriers, Xilinx releases Vitis unified software platform for developers
股票开户佣金优惠,炒股开户哪家证券公司好网上开户安全吗
The shell script reads the redis command in the text and inserts redis in batches
On juicefs
C语言:扫雷小游戏
使用解构交换两个变量的值
[sword finger offer] interview question 54: the k-largest node of the binary search tree
/Dev/loop1 takes up 100% of the problem
go语言慢速入门——go运算符
C language: function stack frame
Is low code the future of development? On low code platform
Use deconstruction to exchange the values of two variables
线程间等待与唤醒机制、单例模式、阻塞队列、定时器
Summary of network device hard core technology insider router (Part 2)
NPM install error unable to access
JS operation DOM node
【剑指offer】面试题41:数据流中的中位数——大、小堆实现
Jump to the specified position when video continues playing
synchronized和ReentrantLock的区别