Leetcode 91 Decode Ways

本文深入探讨了数字解码问题,给出了两种解决方案:递归深度优先搜索和动态规划。通过对比,阐述了动态规划在时间复杂度上的优势,并提供了详细的代码实现。

Problem:

A message containing letters from A-Z is being encoded to numbers using the following mapping:

‘A’ -> 1
‘B’ -> 2

‘Z’ -> 26
Given a non-empty string containing only digits, determine the total number of ways to decode it.

Analysis:

when doing a code problem, I think we can begin code until we figure out the right solution, or we just waste time.
how to make sure we are clear? draw it, write in psydo code, or even write in speaking lanuage.

In this problem, we need to pay attention on the return situation.
method1:
dfs, (recursive)
time complexity T(n) = 2T(n-1) + C, so O(n^2)
space complexity: O(n)

method2:
dfs+DP
storing the value into an array, will save lots of time.
time complexity: O(n)
space complexity: O(n)


public class Solution1 {
	
	
	public static int numDecodings(String s) {
        // corner case
		if (s.length() == 0 || s == null) return 0;		
		return numDecode(s.toCharArray(), 0);	
	}
	
	public static int numDecode(char[] arr, int startPos) {
		// we should pay attention on the return situation
		if (startPos == arr.length) return 1;
		
		int count = 0;
		if (arr[startPos] != '0') {
			count += numDecode(arr, startPos+1);				
		}
		
		if (isValid(arr, startPos)) {
			count += numDecode(arr, startPos+2);
		}
		
		return count;
	}
	
	
	// there is nothing new here, just check whether two characters are in 10-26
	public static boolean isValid(char[] arr, int startPos) {
		
		if (startPos + 1 > arr.length - 1) return false;
		char a = arr[startPos];
		char b = arr[startPos + 1];
		int aNum = a - '0';
		int bNum = b - '0';
		if (aNum * 10 + bNum <= 26 && aNum * 10 + bNum >= 10) {
			return true;
		}
		return false;
	}
	
	
	// in this method, we will use Dynamic Programming
	// we can store the value in the array, so we don't need to calculate it iteratively
	public static int numDecodings1(String s) {

		if (s.length() == 0 || s == null) return 0;		
		
		// we need to store the value when s.length() == 0, so we create the array with length + 1
		int[] note = new int[s.length() + 1];
		
		Arrays.fill(note, -1);
		return numDecode1(note, s.toCharArray(), 0);	
	}
	

	public static int numDecode1(int[] note, char[] arr, int startPos) {
		if (note[startPos] != -1) return note[startPos];
		if (startPos == arr.length) { 
			note[startPos] = 1;
			return 1;
		}
		
		int count = 0;
		if (arr[startPos] != '0') {
			count += numDecode1(note, arr, startPos+1);				
		}
		
		if (isValid(arr, startPos)) {
			count += numDecode1(note, arr, startPos+2);
		}
		note[startPos] = count;
		System.out.println(note[0]);
		return count;
	}
	

	
	public static void main(String[] args) {
		String s = "226";
		System.out.println(numDecodings1(s));
	}
	
}
LeetCode 91题是一道非常经典的动态规划问题,题目名为"解码方法"(Decode Ways)。给定一个只包数字的非空,求解有多少种解码方式具体来说给定的字符串由数字字符组成,可以解码成字母。规定字母'A'对应数字1,字母'B对应数字2,以此类推,字母'Z'对应数字26。现在要求计算出给定字符串的所有可能的解码方式数量。 解决这个问题的一种常见方法是使用动态规划。我们可以定义一个长度为n+1的数组dp,其中dp[i]表示字符串的前i个字符的解码方式数量。根据题目要求,我们需要考虑以下几种情况: 1. 如果当前字符为0,那么它不能单独解码,必须与前一个字符组合起来解码。如果前一个字符为1或2,那么可以将当前字符与前一个字符组合起来解码,即dp[i] = dp[i-2];否则,无法解码,返回0。 2. 如果当前字符不为0,那么它可以单独解码成一个字母,即dp[i] = dp[i-1]。同时,如果前一个字符和当前字符组合起来可以解码成一个字母(范围在1到26之间),那么也可以将前两个字符一起解码,即dp[i] += dp[i-2]。 最终,我们可以通过遍历字符串的每个字符,更新dp数组的值,最后返回dp[n]即可得到解码方式的数量。 下面是Java语言的示例代码: ```java public int numDecodings(String s) { int n = s.length(); int[] dp = new int[n + 1]; dp[0] = 1; dp[1] = s.charAt(0) == '0' ? 0 : 1; for (int i = 2; i <= n; i++) { int oneDigit = Integer.parseInt(s.substring(i - 1, i)); int twoDigits = Integer.parseInt(s.substring(i - 2, i)); if (oneDigit >= 1 && oneDigit <= 9) { dp[i] += dp[i - 1]; } if (twoDigits >= 10 && twoDigits <= 26) { dp[i] += dp[i - 2]; } } return dp[n]; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值