010 Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'.
'.' Matches any single character.
'*' Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).
The function prototype should be:
bool isMatch(const char *s, const char *p)
Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true
详见:https://leetcode.com/problems/regular-expression-matching/description/
方法一:
class Solution {
public:
bool isMatch(string s, string p) {
if(p.empty())
{
return s.empty();
}
if(p.size()==1)
{
return (s.size()==1&&(s[0]==p[0]||p[0]=='.'));
}
if(p[1]!='*')
{
if(s.empty())
{
return false;
}
return (s[0]==p[0]||p[0]=='.')&&isMatch(s.substr(1),p.substr(1));
}
while(!s.empty()&&(s[0]==p[0]||p[0]=='.'))
{
if(isMatch(s,p.substr(2)))
{
return true;
}
s=s.substr(1);
}
return isMatch(s,p.substr(2));
}
};
方法二:动态规划,dp[i][j] 表示 s[0..i] 和 p[0..j] 是否 match
class Solution {
public:
bool isMatch(string s, string p) {
int m = s.size(), n = p.size();
vector<vector<bool>> dp(m + 1, vector<bool>(n + 1, false)); dp[0][0] = true;
for (int i = 1; i <= m; i++)
{
dp[i][0] = false;
}
for (int j = 1; j <= n; j++)
{
dp[0][j] = j > 1 && '*' == p[j - 1] && dp[0][j - 2];
} for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
if (p[j - 1] != '*')
{
dp[i][j] = dp[i - 1][j - 1] && (s[i - 1] == p[j - 1] || '.' == p[j - 1]);
}
else
{
dp[i][j] = dp[i][j - 2] || (s[i - 1] == p[j - 2] || '.' == p[j - 2]) && dp[i - 1][j];
}
}
} return dp[m][n];
}
};
参考:http://www.cnblogs.com/grandyang/p/4461713.html
010 Regular Expression Matching 正则表达式匹配的更多相关文章
- [LeetCode] Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- [LeetCode] 10. Regular Expression Matching 正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- [LeetCode]10. Regular Expression Matching正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- 10. Regular Expression Matching正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- [leetcode]10. Regular Expression Matching正则表达式的匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- No.010 Regular Expression Matching
10. Regular Expression Matching Total Accepted: 89193 Total Submissions: 395441 Difficulty: Hard Imp ...
- LeetCode--No.010 Regular Expression Matching
10. Regular Expression Matching Total Accepted: 89193 Total Submissions: 395441 Difficulty: Hard Imp ...
- 【JAVA、C++】LeetCode 010 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 10. Regular Expression Matching字符串.*匹配
[抄题]: Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
随机推荐
- gulp之压缩css
/** * css压缩 * npm install --save-dev gulp-minify-css * npm install --save-dev gulp-rename * * * 可参考: ...
- 洛谷【P1561】[USACO12JAN]爬山Mountain Climbing
我对\(Jhonson\)算法的理解:https://www.cnblogs.com/AKMer/p/9863620.html 题目传送门:https://www.luogu.org/problemn ...
- Poj_1045
这道题难点在于基本物理知识和数学的结合. 得出公式后再code,那就是小菜一碟了. import java.util.Scanner; import java.lang.Math; public cl ...
- 优秀开源项目之三:高性能、高并发、高扩展性和可读性的网络服务器架构State Threads
译文在后面. State Threads for Internet Applications Introduction State Threads is an application library ...
- 【转】 Pro Android学习笔记(七六):服务(1):local和remote
文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ Android提供服务,服务是运行在后台的 ...
- 【转】 Pro Android学习笔记(三一):Menu(2):扩展、图片、子菜单
目录(?)[-] 菜单扩展 菜单项加入图片 子菜单 菜单扩展 如果菜单项很多,超过六个时,就会采用菜单扩展模式.在例子中我加入了10个菜单项,预计能进入菜单扩展模式,但是实际效果如右图所示.效果和li ...
- JAVA 编程思想三
1:JAVA可变参数? 参数个数不确定,但是类型确定: 可变参数位于最后一项,只支持一个可变参数: public void funciton1(int a, String ...args) { for ...
- Python模块-subprocess模块
Run()方法 >>> a = subprocess.run(['df','-h']) 文件系统 容量 已用 可用 已用% 挂载点 udev 468M 0 468M 0% /dev ...
- 使用python对文件夹里面所有代码行数进行统计。
统计目录下所有的代码个数和总行数. # -*- coding: utf-8 -*- # @Author : ydf import json import os from pathlib import ...
- C语言学习笔记--enum和sizeof关键字
1.enum关键字 C语言中enum关键字用来定义枚举类型 (1)enum 是 C 语言中的一种自定义类型(2)enum 值是可以根据需要自定义的的整型值(3)第一个定义的 enum 值默认为 0 ( ...