'.' 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

以下是牛人的实现:

class Solution {
public:
bool isMatch(string s, string p) {
int i, j;
int m = s.size();
int n = p.size();
const char *s1 = s.c_str();
const char *p1 = p.c_str();
/**
* b[i + 1][j + 1]: if s[0..i] matches p[0..j]
* if p[j] != '*'
* b[i + 1][j + 1] = b[i][j] && s[i] == p[j]
* if p[j] == '*', denote p[j - 1] with x,
* then b[i + 1][j + 1] is true iff any of the following is true
* 1) "x*" repeats 0 time and matches empty: b[i + 1][j -1]
* 2) "x*" repeats 1 time and matches x: b[i + 1][j]
* 3) "x*" repeats >= 2 times and matches "x*x": s[i] == x && b[i][j + 1]
* '.' matches any single character
*/
bool b[m + ][n + ];
b[][] = true;
for (i = ; i < m; i++)
{
b[i + ][] = false;
}
// p[0..j - 2, j - 1, j] matches empty iff p[j] is '*' and p[0..j - 2] matches empty
for (j = ; j < n; j++)
{
b[][j + ] = j > && '*' == p1[j] && b[][j - ];
} for (i = ; i < m; i++)
{
for (j = ; j < n; j++)
{
if (p[j] != '*')
{
b[i + ][j + ] = b[i][j] && ('.' == p1[j] || s1[i] == p1[j]);
}
else
{
b[i + ][j + ] = b[i + ][j - ] && j > || b[i + ][j] ||
b[i][j + ] && j > && ('.' == p1[j - ] || s1[i] == p1[j - ]);
}
}
}
return b[m][n];
} };

这应该是用动态规划的方法实现的。

最新文章

  1. 图片大小以及dp和px关系一览表,logo尺寸
  2. python实现查看目录下重复的文件
  3. [大坑]FFT学习
  4. Lintcode: Majority Number II
  5. win32空项目创建窗体
  6. Service的基本组成
  7. oracle中数据类型对应java类型
  8. 玩转C线性表和单向链表之Linux双向链表优化
  9. 手写事务管理器 也是spring实现事务管理的原理
  10. kubeadm简单安装k8s
  11. 【dpdk】使用libpcap-PMD驱动收发包
  12. Vue.js 源码学习笔记 -- 分析前准备2 -- Object.defineProperty
  13. 10.Curator队列
  14. 201621123010《Java程序设计》第5周学习总结
  15. 转:函数指针数组的妙用(I)
  16. LeetCode(476): Number Complement
  17. &quot;ImportError: cannot import name OVSLegacyKernelSwitch&quot;
  18. darknet 识别获取结果
  19. 官网下载MySQL
  20. Lightoj 1044 - Palindrome Partitioning (DP)

热门文章

  1. jmeter+ant的使用
  2. 局部敏感哈希LSH
  3. C++ 学习笔记之——文件操作和文件流
  4. TF-IDF与主题模型 - NLP学习(3-2)
  5. 最短路径——Bellman-Ford算法以及SPFA算法
  6. Log4Net讲解
  7. 【bzoj1176】[Balkan2007]Mokia/【bzoj2683】简单题 CDQ分治+树状数组
  8. [CF327E]Axis Walking([洛谷P2396]yyy loves Maths VII)
  9. vue.js 三种方式安装--npm安装
  10. 移动开发:美团外卖Android Lint代码检查实践