题解 | #把数字翻译成字符串#
把数字翻译成字符串
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 if (nums == null || nums.length() < 1) { return 0; } char[] arr = nums.toCharArray(); if (arr[0] == '0') { return 0; } int n = arr.length; int[] dp = new int[n]; dp[0] = 1; for (int i = 1; i < n; i++) { if (arr[i] == '0' && (arr[i - 1] != '1' && arr[i - 1] != '2')) { return 0; } if (arr[i] != '0') { dp[i] = dp[i - 1]; } if (arr[i - 1] == '1' || (arr[i - 1] == '2' && arr[i] <= '6')) { dp[i] += (i > 1 ? dp[i - 2] : 1); } } return dp[n - 1]; } }