当前位置:网站首页>Brute force recursion to dynamic programming 04 (digital string conversion)
Brute force recursion to dynamic programming 04 (digital string conversion)
2022-07-29 23:22:00 【Taotao can't learn English】
1. 题目

2. 暴力递归
Determine the termination conditions,If the selection ends at the last digit,Show that this choice is correct,返回1,如果遇到0,A single position cannot be changed to a letter;
Get the result of selecting the last bit first,Then determine whether there is a combination of the latter two,相加即可.
public int number(String str) {
return getNumber(str.toCharArray(), 0);
}
private int getNumber(char[] chars, int curIndex) {
//If the last digit is reached,则返回 1
if (curIndex == chars.length) {
return 1;
}
//One can choose to change 或 change to two
//Become one
//如果当前值为 数字 0 ,There is no corresponding letter
if (chars[curIndex] == '0') {
return 0;
}
int ways = getNumber(chars, curIndex + 1);
//change to two,一共26个字母
if (curIndex + 1 < chars.length && (chars[curIndex] - '0') * 10 + (chars[curIndex + 1] - '0') < 27) {
ways += getNumber(chars, curIndex + 2);
}
return ways;
}
3. 动态规划
Dynamic programming can be evolved from brute force recursion,dpArrays are the only variables curIndex 的变化范围,为 [ 0 , chars.lengrth]
初始条件为 dp[ curIndex ] = 1,即终止条件
The rest is to change according to greed.
public int number2(String str) {
return getNumber2(str.toCharArray());
}
private int getNumber2(char[] chars) {
int[] dp = new int[chars.length + 1];
//Only the current position is a variable
// 最后一位为1
dp[chars.length] = 1;
for (int curIndex = chars.length - 1; curIndex >= 0; curIndex--) {
//This bit is not0
if (chars[curIndex] != '0') {
//This bit can be changed to a single letter
int ways = dp[curIndex + 1];
if (curIndex + 1 < chars.length && ((chars[curIndex] - '0') * 10 + (chars[curIndex + 1] - '0')) < 27) {
ways += dp[curIndex + 2];
}
dp[curIndex] = ways;
}
}
return dp[0];
}
public static void main(String[] args) {
System.out.println(new StrConvert().number("111"));
System.out.println(new StrConvert().number2("111"));
}
边栏推荐
猜你喜欢

This article penetrates the architecture design and cluster construction of the distributed storage system Ceph (hands-on)

很遗憾,没有一篇文章能讲清楚分布式事务

A print function, very good at playing?

Super RVRT

VsCode更新后,怎么使用使用快捷键同时生成多个元素

「大厂必备」系列之Redis主从、持久化、哨兵

BGP联邦综合实验

【openlayers】地图【一】

【openlayers】地图【二】

BGP Federal Comprehensive Experiment
随机推荐
Sort by a field in jsonArray
50. Leetcode 】 【 Pow (x, n) (medium) (power) quickly
Codeforces Round #245 (Div. 1) A (dfs)
新版微信小程序发布指南
J9数字论:为什么我们需要Web3?
「大厂必备」系列之Redis主从、持久化、哨兵
2022年最新甘肃建筑施工焊工(建筑特种作业)模拟题库及答案解析
Access the company intranet
华为14天-(3)内核开发
@Accessors 注解详解
Foxmail是什么邮箱?
HRNet-Facial-Landmark-Detection 训练自己数据集
【openlayers】地图【一】
J9 Number Theory: Why do we need Web3?
labview怎么做成应用程序(labview程序识别形状)
设计消息队列存储消息的MySQL表格
【leetcode】The sword refers to Offer II 002. Binary addition
BGP联邦综合实验
【leetcode】剑指 Offer II 006. 排序数组中两个数字之和(二分查找、双指针)
The first round of the real offer harvester~ How does the big factory inspect the candidates?(with detailed answer)