Source: Niuke and Likou
Disclaimer: If I violate anyone's rights, please contact me and I will delete it.
Welcome experts to spray me
Article Directory
Sword refers to Offer 46. Translate numbers into strings
Link: https://leetcode-cn.com/problems/ba-shu-zi-fan-yi-cheng-zi-fu-chuan-lcof
Title description
Given a number, we translate it into a string according to the following rules: 0 is translated into "a", 1 is translated into "b", ..., 11 is translated into "l", ..., 25 is translated into "z". There may be multiple translations for a number. Please program a function to calculate how many different translation methods a number has.
Example 1:
Input: 12258
Output: 5
Explanation: 12258 has 5 different translations, namely "bccfi", "bwfi", "bczi", "mcfi" and "mzi"
Code
Ideas:
We can use f(i) to denote the number of translation schemes for the prefix string ending at the i-th position, considering the contribution of the i-th independent translation and the connection with the previous one and then translation to f(i). The contribution of a single translation to f(i) is f(i-1);
if the i-1 bit exists, and the number x formed by the i-1 bit and the i bit satisfies 10≤x≤25, then you can The i-1 bit and the i bit are translated together, and the contribution to f(i) is f(i-2), otherwise it is 0. We can list such a dynamic programming transition equation:
f(i) = f(i-1) + f(i-2) The
boundary conditions are f(-1) = 0, f(0) = 1. [C] in the equation means [c] = 1 when c is true, otherwise [c] = 0
class Solution {
public int translateNum(int num) {
String str = String.valueOf(num);
char[] ch = str.toCharArray();
int len = ch.length;
int[]dp = new int[len];
dp[0] = 1;
for(int i=1;i<len;++i){
//if(ch[i-1]=='0') {dp[i]=dp[i-1];continue;}
int tem = (ch[i-1]-'0')*10 + (ch[i]-'0');
if(tem<26 && tem>=10){
if(i>1) dp[i] = dp[i-1]+dp[i-2];
else dp[i] = dp[i-1]+1;
}else{
dp[i] = dp[i-1];
}
//System.out.println(tem);
}
//System.out.println(Arrays.toString(dp));
return dp[len-1];
}
}
Niuke translates numbers into strings
Title description
There is a way to encode letters into numbers:'a'->1,'b->2',…,'z->26'.
Now give a string of numbers, how many possible decoding results are returned
Example 1
Input "12" and
return value 2
shows 2 possible decoding results ("ab" or "l")
Example 2
Input "31717126241541717" and the
return value 192
shows 192 possible decoding results
Code
import java.util.*;
public class Solution {
/**
* 解码
* @param nums string字符串 数字串
* @return int整型
*/
public int solve (String nums) {
// write code here
if(nums== null || nums.length()==0) return 0;
if (nums.charAt(0) == '0'){
return 0;
}
char[]chs = nums.toCharArray();
int len = nums.length();
int[]dp = new int[len];
dp[0] = 1;
for(int i=1;i<len;++i){
int x = (chs[i-1]-'0') * 10 + (chs[i]-'0');
if(chs[i]-'0' == 0){
if(x <= 26 && x >=10){
dp[i] = dp[i-1];
}
}else{
if(x <=26 && x >= 10){
if(i>1) dp[i] = dp[i-1] + dp[i-2];
else dp[i] = dp[i-1]+1;
}else{
dp[i] = dp[i-1];
}
}
}
return dp[len-1];
}
}
The legality of the difference 0 between the two questions
- The question on the force button is 0=》a, the range is 0-25
- Question 1=》a on Niuke, the range is 1-26, 0 is illegal, so the question on Niuke needs to discuss 0:
First define the state dp[i] as the number of results for the first i characters, then the state transition equation has (note the influence of leading zeros, such as "01" is illegal and cannot be converted to'a'.):
when str[i 】== '0', if "10" <= str[i-1] str[i] <= "26": dp[i] = dp[i-1]
when str[i]!= '0' ,If "10" <= str[i-1] str[i] <= "26": dp[i] = dp[i-1] + dp[i-2], otherwise dp[i] = dp[ i-1].