当前位置:网站首页>【剑指offer】面试题46:把数字翻译成字符串——动态规划
【剑指offer】面试题46:把数字翻译成字符串——动态规划
2022-07-27 14:24:00 【Jocelin47】

方法一:动态规划
通过建立转移方程 f(i) = f(i-1) + g(i,i-1)*f(i-2)
如果前两个数在10-25之间,则g(i,i-1)为1,否则为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()];
}
};
边栏推荐
- How to take satisfactory photos / videos from hololens
- Network equipment hard core technology insider router Chapter 9 Cisco asr9900 disassembly (II)
- 学习Parquet文件格式
- EMC design scheme of USB2.0 Interface
- 西瓜书《机器学习》阅读笔记之第一章绪论
- Unity3d learning note 10 - texture array
- Leetcode 781. rabbit hash table in forest / mathematical problem medium
- Leetcode 90. subset II backtracking /medium
- ADB command (install APK package format: ADB install APK address package name on the computer)
- Network equipment hard core technology insider router Chapter 4 Jia Baoyu sleepwalking in Taixu Fantasy (Part 2)
猜你喜欢

generic paradigm

Unity performance optimization ----- LOD (level of detail) of rendering optimization (GPU)

With just two modifications, apple gave styleganv2 3D generation capabilities

Spark 本地程序启动缓慢问题排查

光电隔离电路设计方案(六款基于光耦、AD210AN的光电隔离电路图)

华云数据打造完善的信创人才培养体系 助力信创产业高质量发展

Introduction of the connecting circuit between ad7606 and stm32
![[0 basic operations research] [super detail] column generation](/img/cd/f2521824c9ef6a50ec2be307c584ca.png)
[0 basic operations research] [super detail] column generation

Leetcode-1737- minimum number of characters to change if one of the three conditions is met

Leetcode-1737-满足三条件之一需改变的最少字符数
随机推荐
Usage of countdownlatch in multithreaded environment
Basic usage of kotlin
Network equipment hard core technology insider router Chapter 4 Jia Baoyu sleepwalking in Taixu Fantasy (Part 2)
适配验证新职业来了!华云数据参与国家《信息系统适配验证师国家职业技能标准》编制
generic paradigm
Discussion on STM32 power down reset PDR
Digital storage oscilloscope based on FIFO idt7202-12
Leetcode-1737-满足三条件之一需改变的最少字符数
STL value string learning
Four kinds of relay schemes driven by single chip microcomputer
reflex
Leetcode 191. number of 1 bits bit operation /easy
After configuring corswebfilter in grain mall, an error is reported: resource sharing error:multiplealloworiginvalues
js运用扩展操作符(…)简化代码,简化数组合并
一文读懂鼠标滚轮事件(wheelEvent)
《终身成长》读书笔记(一)
STM32 can communication filter setting problem
Problem solving in magic tower project
Unity performance optimization ----- LOD (level of detail) of rendering optimization (GPU)
QT (IV) mixed development using code and UI files