leetcode 10.正则表达式
题目描述特别简洁: 先贴代码: class Solution { public : bool isMatch(string s ,string p){ int dp[100][100] = {0}; s.insert(0,1,'@');p.insert(0,1,'@'); //为了将空字符考虑在内,初始化插入@方便之后的操作 int ls = s.size(),lp = p.size(); dp[0][0] = 1; //初始化dp(p与s为空字符) for(int j = 1 ; j < lp; j++){ for(int i = 0; i < ls; i ++){ if(i != 0 && (s[i] == p[j] || p[j] == '.') && dp[i-1][j-1] == 1 ){ //直接匹配 dp[i][j] = 1; } else if(p[j] == '*') //当p[j]为*时情况较为复杂 { if(j - 2 >= 0 && dp[i][j - 2] == 1) //可以直接把零个随意字符通配Σ*忽略 { dp[i][j] = 1; } else if(i != 0 && (dp[i - 1][j] == 1 || dp[i - 1][j - 1] == 1) && (s[i] == p[j - 1] || p[j-1] == '.')) //将Σ