【LeetCode笔记】Regular Expression Matching

本文深入探讨了正则表达式的匹配算法,通过递归和动态规划两种方法进行实现与优化。递归方法提供了一种简洁的解决方案,而动态规划方法则在效率上进行了显著提升。同时,详细解释了*字符的特殊匹配规则及其实现细节。

Analysis: If there is no * followed, just simple match

if there is a * followed, two scenarios: 1. * and its preceding char are not need; 2. * matches its preceding char for 0~N times


Implementations:

1. Recursive method:

bool isMatch(const char *s, const char *p) {
	//p is empty
	if(*p == '\0')
		return (*s == '\0');
		
	//p is not empty
	//a * is followed
	if(*(p + 1) == '*'){
		//take 0 preceding element
		if(isMatch(s, p + 2))
			return true;
		//take 1 or more preceding elements
		else{
			while(*s != '\0' && (*s == *p || *p == '.')){
				if(isMatch(s + 1, p + 2))
					return true;
				else
					s++;
			}
		}
	}
	//no * is followed
	else
		if(*s != '\0' && (*s == *p || *p == '.'))
			return isMatch(s + 1, p + 1);
	return false;
}
116ms


2. DP method

bool isMatch(const char *s, const char *p){
	//get lenght of s and p
	int m = 0, n = 0;
	while(s[m] != '\0')
		m++;
	while(p[n] != '\0')
		n++;
			
	//dp[i][j]: substring s[0..i-1] and p[0..j-1] match or not
	vector<vector<bool> > dp(m + 1, vector<bool>(n + 1, false));	
	dp[0][0] = true; //s[0..-1] and p[0..-1] are empty. match
	for(int i = 1; i <= m; ++i) //p is empty, s is not. never match
		dp[i][0] = false;
	for(int j = 2; j <= n; ++j)//s is empty, p is not. only p in pattern (c *)^k can match, such as a*b*c*
		if(p[j - 1] == '*' && dp[0][j - 2])
			dp[0][j] = true;
			
	//dp method			
	for(int i = 1; i <= m; ++i)
		for(int j = 1; j <= n; ++j){
			//simple match for no *
			if(p[j - 1] != '*'){
				dp[i][j] = ((s[i - 1] == p[j - 1] || p[j - 1] == '.') && dp[i - 1][j - 1]);
			}
			//two scenarios for *
			if(p[j - 1] == '*'){
				dp[i][j] = //1. do not need (c *) to match
				           (j >= 2) && dp[i][j - 2]  
				           //2. need (c *) to match
						|| dp[i][j - 1]  && (s[i - 1] == p[j - 2] || p[j - 2] == '.')  //* matches empty        
						|| dp[i - 1][j]  && (s[i - 1] == p[j - 2] || p[j - 2] == '.'); //* matches c 				
			}
		}
	return dp[m][n]; //dp[m][n]: s[0..m-1] and p[0..n-1] match or not	
}
60ms
### 关于 LeetCode 学习笔记及相关资源 对于希望深入学习并通过 LeetCode 提升编程能力的人来说,找到合适的笔记和资源是非常重要的。以下是关于 LeetCode 笔记本或学习笔记 PDF 的相关内容: #### 1. **LeetCode 题目与 Python 编程** Python 是一种广泛应用于算法练习的语言,许多开发者通过它来解决 LeetCode 上的各种问题[^1]。这些笔记通常会记录解题思路、代码实现以及可能的优化方向。 #### 2. **高效记录与整理编程学习笔记的方法** 为了更好地掌握 LeetCode 中的知识点,可以采用一些高效的笔记记录方式。这不仅有助于巩固所学内容,还能够方便后续查阅。例如: - 使用 Markdown 文件创建结构化的文档。 - 将每道题目分为几个部分:问题描述、解决方案分析、代码实现及复杂度评估[^2]。 #### 3. **获取 LeetCode 笔记本或 PDF 资源的方式** 虽然网络上存在大量免费的学习资料,但需要注意版权问题。如果想下载高质量的 LeetCode 学习笔记 PDF 或其他形式的资源,可以通过以下途径查找: - 访问 GitHub 平台,在其中搜索关键词如 “LeetCode solutions” 或者特定语言版本(比如 Python)的相关仓库。 - 加入技术社区或者论坛讨论区,与其他成员分享彼此制作的优质笔记文件。 - 利用搜索引擎输入精确查询词组尝试定位到公开发布的电子书链接地址[^3]。 ```python # 示例代码片段展示如何保存本地Markdown格式化后的数据作为PDF导出功能之一 from markdown import markdown import pdfkit def save_as_pdf(text, output_filename='output.pdf'): html_text = markdown(text) config = pdfkit.configuration(wkhtmltopdf="C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe") # 替换为你实际安装路径 options = { 'page-size': 'Letter', 'margin-top': '0.75in', 'margin-right': '0.75in', 'margin-bottom': '0.75in', 'margin-left': '0.75in' } with open(output_filename,'wb') as f: pdf_content=pdfkit.from_string(html_text,False,options=options,configuration=config) f.write(pdf_content) example_note=""" # 动态规划入门经典案例 Fibonacci Sequence (斐波那契数列) 给定正整数 n ,返回第n项Fibonacci数值... """ save_as_pdf(example_note,"fibonacci_example.pdf") ``` 上述脚本演示了一个简单的例子,说明怎样把一段文字转换成HTML再进一步加工成为PDF档存盘操作过程[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值