Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
1 Star 1 Fork 0

dxlearn/Algorithm

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
master
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
The license selected for the repository is subject to the license used by the main branch of the repository.
master
Branches (1)
master
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
master
Algorithm
/
DP
/
Translation.java
Algorithm
/
DP
/
Translation.java
Translation.java 1.94 KB
Copy Edit Raw Blame History
dxlearn authored 2022年05月15日 15:24 +08:00 . update "动态规划-数字翻译成字符串"
package DP;
import java.util.Scanner;
/**
* @author dx
* @version 1.0
* @date 2022年5月14日 11:21
* @description:剑指offer-46 把数字翻译成字符串
*
* dp[i]:表示前i个数字的翻译方法可能个数
* num = X1X2X3....Xi-1Xi...Xn-1Xn
* 例如:12258 = X1X2X3X4X5
* 设 X1X2...Xi-2的翻译方案数量为f(i-2)
* 设X1X2...X
*/
public class Translation {
public static void main(String[] args) {
Scanner sr = new Scanner(System.in);
int num = sr.nextInt();
System.out.println(new Tracnslation_Solution().translateNum(num));
System.out.println(new Tracnslation_Solution().translateNum2(num));
}
}
class Tracnslation_Solution{
public int translateNum(int num){
String s = String.valueOf(num); //int 转换成string
//初始化 dp[-1] = 0
int p = 0, q = 0, r = 1;
for(int i = 0; i < s.length();i++){
p = q; // q从上一轮的i-1,变为这一轮的i-2 --->p(i-2)
q = r; //res为上一轮的i,赋值到这一轮的i-1 ---->q(i-1)
r = 0; //r重置
r += q; //r开始累计了,当前先加上i-1
//下面开始尝试,是否要再累加 i-2
if(i ==0){
continue;
}
String sub = s.substring(i-1,i+1);//不包含i+1,也就是i-1 i
if(sub.compareTo("25") <= 0 && sub.compareTo("10") >=0){
r += p;
}
}
return r;
}
public int translateNum2(int num){
String s = String.valueOf(num);
int n = s.length();
int [] dp = new int[n+1];
dp[0] = 1;
for(int i =1;i<=n;i++){
dp[i] = dp[i-1]; //单独翻译
if(i>1){
int t = (s.charAt(i-2) - '0')*10+s.charAt(i-1) - '0';
if(t >=10 && t<=25){
dp[i] += dp[i-2]; //将dp[i]和dp[i-1]组合翻译
}
}
}
return dp[n];
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

No description
Cancel

Releases

No release

Contributors

All

Language(Optional)

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/dxlearn/Algorithm.git
git@gitee.com:dxlearn/Algorithm.git
dxlearn
Algorithm
Algorithm
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /