题解 | #把数字翻译成字符串#
把数字翻译成字符串
https://www.nowcoder.com/practice/046a55e6cd274cffb88fc32dba695668
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 解码 * @param nums string字符串 数字串 * @return int整型 */ public int solve (String nums) { // write code here int len = nums.length(); int[] dp = new int[len+1]; if ((int) nums.charAt(0) - '0' != 0) { dp[1] = 1; } for (int i = 1; i < len; i++) { int pre = (int) nums.charAt(i - 1) - '0'; int tmp = (int) nums.charAt(i) - '0'; if(tmp!=0){ dp[i+1] = dp[i]; } if ((pre == 2 && tmp < 7) || pre == 1) { dp[i+1] += dp[i-1]; if(i==1){ dp[i+1] += 1; } } else { if (tmp == 0) { return 0; } } } return dp[len]; } }