当前位置:网站首页>LeetCode_ Dynamic programming_ Medium_ 91. decoding method
LeetCode_ Dynamic programming_ Medium_ 91. decoding method
2022-06-29 07:14:00 【I've been up and down in the Jianghu】
1. subject
One contains letters A-Z The message is mapped as follows code :
'A' -> "1"
'B' -> "2"
...
'Z' -> "26"
To decode an encoded message , All numbers must be based on the above mapping method , Back to letters ( There may be several ways ). for example ,“11106” It can be mapped to :
① “AAJF” , Group messages into (1 1 10 6)
② “KJF” , Group messages into (11 10 6)
Be careful , Messages cannot be grouped into (1 11 06) , because “06” Cannot map to “F” , This is because “6” and “06” It's not equivalent in mapping .
Give you a non empty string containing only numbers s , Please calculate and return the total number of decoding methods .
The question data guarantee that the answer is definitely one 32 An integer .
Example 1:
Input :s = “12”
Output :2
explain : It can be decoded as “AB”(1 2) perhaps “L”(12).
Example 2:
Input :s = “226”
Output :3
explain : It can be decoded as “BZ” (2 26), “VF” (22 6), perhaps “BBF” (2 2 6) .
Example 3:
Input :s = “0”
Output :0
explain : No characters are mapped to 0 Number at the beginning .
contain 0 The effective mapping of is ‘J’ -> “10” and ‘T’-> “20” .
Because there are no characters , So there is no effective way to decode this , Because all numbers need to be mapped .
Tips :
1 <= s.length <= 100
s Numbers only , And may contain leading zeros .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/decode-ways
2. Ideas
(1) Dynamic programming
hypothesis dp[i] Express s[0…i - 1] Number of decoding methods , And set dp[0] = 1. We can know from the analysis that State transition relationship as follows :
① If s[i] Itself can represent a letter , At this time, the number of decoding methods dp[i] = dp[i - 1];
② If s[i] and s[i - 1] Stitching together can represent a letter , At this time, the number of decoding methods dp[i] = dp[i - 2];
If ① ② At the same time satisfy , be dp[i] = dp[i - 1] + dp[i] = dp[i - 2]; If they are not satisfied , be dp[i] = 0( Default value ).
3. Code implementation (Java)
// Ideas 1———— Dynamic programming
class Solution {
public int numDecodings(String s) {
int length = s.length();
if (length < 1) {
return 0;
}
//dp[i] Express s[0...i - 1] Number of decoding methods
int[] dp = new int[length + 1];
dp[0] = 1;
dp[1] = s.charAt(0) == '0' ? 0 : 1;
for (int i = 2; i <= length; i++) {
char c1 = s.charAt(i - 1);
char c2 = s.charAt(i - 2);
if (c1 >= '1' && c1 <= '9') {
// s[i] Itself can represent a letter
dp[i] += dp[i - 1];
}
if (c2 == '1' || c2 == '2' && c1 <= '6') {
// s[i] and s[i - 1] Stitching together can represent a letter
dp[i] += dp[i - 2];
}
}
return dp[length];
}
}
边栏推荐
- Mongostat performance analysis
- Testing grpc service with grpcui
- Illegal forward reference and enums
- NoSQL数据库之Redis(二):Redis配置文件介绍
- json tobean
- 存token获取token刷新token发送header头
- [answer all questions] CSDN question and answer function evaluation
- QT program packaging and publishing windeployqt tool
- 你真的懂 “Binder 一次拷贝吗“?
- RPC and RMI
猜你喜欢
![[when OSPF introduces direct connection routes, it makes a summary by using static black hole routes]](/img/a8/f77cc5e43e1885171e73f8ab543ee4.png)
[when OSPF introduces direct connection routes, it makes a summary by using static black hole routes]

Redis (4) of NoSQL database: redis new data type

QT qlineedit details

Domestic code hosting center code cloud

关联性——相关性分析

Idea integrated code cloud

Li Kou today's question -324 Swing sort II

Testing grpc service with grpcui

mongostat性能分析

Error: GPG check FAILED Once install MySQL
随机推荐
How to do the performance pressure test of "Health Code"
As a qualified network worker, you must master DHCP snooping knowledge!
VerilogA - counter
Datatables屏蔽报错弹窗
NoSQL数据库介绍
Redis of NoSQL database (I): Installation & Introduction
Qt 程序打包发布-windeployqt工具
JVM系列之对象深度探秘
. NETCORE uses redis to limit the number of interface accesses
And check the collection hello
Some thoughts on port forwarding program
更改主机名的方法(永久)
消息队列之通过幂等设计和原子锁避免重复退款
把多个ROC曲线画在一张图上
IDEA常用插件
The realization of changing pop-up background at any time
NoSQL數據庫之Redis(五):Redis_Jedis_測試
Li Kou today's question -324 Swing sort II
Vite quick start
What is the difference between software engineer and software development? What is the difference between software engineer and software developer?