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));
	}
	
}
【顶级EI复现】计及连锁故障传播路径的电力系统 N-k 多阶段双层优化及故障场景筛选模型(Matlab代码实现)内容概要:本文介绍了名为《【顶级EI复现】计及连锁故障传播路径的电力系统 N-k 多阶段双层优化及故障场景筛选模型(Matlab代码实现)》的研究资源,重点围绕电力系统中连锁故障的传播机制,提出了一种N-k多阶段双层优化模型,并结合故障场景筛选方法提升系统安全性与鲁棒性。该模型通过Matlab代码实现,可用于模拟复杂电力系统在多重故障下的响应特性,支持对关键故障路径的识别与优化决策,适用于高水平科研复现与工程仿真分析。文中还列举了大量相关技术方向的配套资源,涵盖智能优化算法、电力系统管理、机器学习、路径规划等多个领域,并提供了网盘链接以便获取完整代码与资料。; 适合人群:具备电力系统、优化理论及Matlab编程基础的研究生、科研人员及从事能源系统安全分析的工程技术人员,尤其适合致力于高水平论文(如EI/SCI)复现与创新的研究者。; 使用场景及目标:①复现顶级期刊关于N-k故障与连锁传播的优化模型;②开展电力系统韧性评估、故障传播分析与多阶段防御策略设计;③结合YALMIP等工具进行双层优化建模与场景筛选算法开发;④支撑科研项目、学位论文或学术成果转化。; 阅读建议:建议读者按照文档提供的目录顺序系统学习,优先掌握双层优化与场景筛选的核心思想,结合网盘中的Matlab代码进行调试与实验,同时参考文中提及的智能算法与电力系统建模范例,深化对复杂电力系统建模与优化的理解。
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]; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值