Regular Expression Matching leetcode
递归方法运行时间过长。考虑使用动态规划的方法。
代码如下:
bool isMatch(string s, string p) {
int i,j;
int m=s.size();
int n=p.size();
bool b[m + ][n + ]; // b[i][j]表示s[i-1]和p[j-1]的匹配结果
b[][] = true;
for (i = ; i < m; i++) {
b[i + ][] = false;
}
for (j = ; j < n; j++) {
b[][j + ] = j > && '*' == p[j] && b[][j - ];
}
for (i = ; i < m; i++) {
for (j = ; j < n; j++) {
if (p[j] != '*') {
b[i + ][j + ] = b[i][j] && ('.' == p[j] || s[i] == p[j]); //表达式中的b[i][j]的意思是:s[i-1]已经和p[j-1]匹配。下同。
} else {
b[i + ][j + ] = b[i + ][j - ] && j > || b[i + ][j] ||
b[i][j + ] && j > && ('.' == p[j - ] || s[i] == p[j - ]); //别分为*之前的字符匹配零次、一次、多次
}
}
}
return b[m][n];
}
Regular Expression Matching leetcode的更多相关文章
- Regular Expression Matching leetcode java
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- lc面试准备:Regular Expression Matching
1 题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single char ...
- [LeetCode] Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- LeetCode | Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
- LeetCode (10): Regular Expression Matching [HARD]
https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...
- [Leetcode][Python][DP]Regular Expression Matching
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...
- [LeetCode][Python]Regular Expression Matching
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...
- 【一天一道LeetCode】#10. Regular Expression Matching
一天一道LeetCode系列 (一)题目 Implement regular expression matching with support for '.' and '*'. '.' Matches ...
- [leetcode]Regular Expression Matching @ Python
原题地址:https://oj.leetcode.com/problems/regular-expression-matching/ 题意: Implement regular expression ...
随机推荐
- Linux 集群
html,body { } .CodeMirror { height: auto } .CodeMirror-scroll { } .CodeMirror-lines { padding: 4px 0 ...
- JS面向对象的程序设计
面向对象的语言有一个标志,即拥有类的概念,抽象实例对象的公共属性与方法,基于类可以创建任意多个实例对象,一般具有封装.继承.多态的特性!但JS中对象与纯面向对象语言中的对象是不同的,ECMA标准定义J ...
- C#打开文件对话框
OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = System.Environment.CurrentDirector ...
- EBS中启用OAF页面个性化三个配置
启用OAF页面个性化三个配置(Profiles) FND:诊断英文为FND: Diagnostics,用于设置是否显示“关于此页” 个性化自助定义英文为Personalize Self-Service ...
- CoreData总结
Core Data,它提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite数据库文件中,也能够将保存在数据库中的数据还原成OC对象.在此数据操作期间,我们不需要编写任何 ...
- 用CMake设置Visual Studio工程中预处理器定义值
构建VS工程时预处理值是不可缺少的,如动态库的导出配置等.在通过CMake构建VS工程时,可以通过CMake命令进行定义,下面讲三种应用. 字符集:默认装填下VS工程是多字节字符集,如果需要使用Uni ...
- ASP.NET Misconfiguration: Missing Error Handling
Abstract: An ASP .NET application must enable custom error pages in order to prevent attackers from ...
- error-2016-2-15
错误:该请求包含双重转义序列,而 Web 服务器上配置的请求筛选拒绝双重转义序列原因:一些URL中可能会包含+号等符号,然后IIS7以上的版本会默认拒绝请求此URL,需要进行如下的修改. 解决PHP中 ...
- NetBIOS发包
[NetBIOS发包] 1.拥有 LANA.Local Session Num即可发包. ncb_lsn,session号.指定发向哪. ncb_lana_num,lan-adapter号,指定用哪一 ...
- python动态获取对象的属性和方法 (转载)
首先通过一个例子来看一下本文中可能用到的对象和相关概念. #coding:utf-8 import sys def foo():pass class Cat(object): def __init__ ...