标签:equals highlight 不为 模式 字符串 java class nbsp new
问题: S代表原字符串,P代表模式串,包含* (匹配0或者多个)和?(匹配一个)
思路:动态规划
class Solution {
public boolean isMatch(String s, String p) {
if(null==s || null==p) return false;
if("*".equals(p) || s.equals(p)) return true;
int sl = s.length(), pl=p.length();
if(pl==0) return false; //模式串为空,匹配串不为空
boolean[][] dp = new boolean[sl+1][pl+1];
char[] sc = s.toCharArray();
char[] pc = p.toCharArray();
dp[0][0] =true;
if(pc[0]==‘*‘) {
int j=0;
while(j<pl && pc[j++]==‘*‘) dp[0][j]=true; //处理模式串“***。。”的情况
for(int i=1;i<=sl;i++) dp[i][0]=true;
}
for(int i =1;i<=sl;i++){
for(int j=1;j<=pl;j++){
if(pc[j-1]==‘*‘) dp[i][j] = dp[i-1][j] || dp[i][j-1] || dp[i-1][j-1];
else if(pc[j-1]==sc[i-1] || pc[j-1]==‘?‘) dp[i][j] = dp[i-1][j-1];
}
}
return dp[sl][pl];
}
}
标签:equals highlight 不为 模式 字符串 java class nbsp new
原文地址:https://www.cnblogs.com/zzm96/p/12896545.html