【leetcode】第44题 Wildcard Matching 题目+解析+代码
2017-08-23 12:08
429 查看
【题目】
Implement wildcard pattern matching with support for
【解析】
这个题和第10题很像,就是‘?’可以代表任意一个字符,‘*’可以代表零个或任意多个任意字符。
这里用动态规划解答。
【代码】
public boolean isMatch(String s, String p) {
int m=s.length(),n=p.length();
boolean[][] match = new boolean[m+1][n+1];
match[0][0] = true;
for(int i=0;i<n;i++){
if(p.charAt(i)=='*')
match[0][i+1]=match[0][i];
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(p.charAt(j)=='?'||p.charAt(j)==s.charAt(i))
match[i+1][j+1]=match[i][j];
else if(p.charAt(j)=='*')
match[i+1][j+1]=match[i+1][j]||match[i][j+1];
}
}
return match[m]
;
Implement wildcard pattern matching with support for
'?'and
'*'.
'?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). 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", "*") → true isMatch("aa", "a*") → true isMatch("ab", "?*") → true isMatch("aab", "c*a*b") → false
【解析】
这个题和第10题很像,就是‘?’可以代表任意一个字符,‘*’可以代表零个或任意多个任意字符。
这里用动态规划解答。
【代码】
public boolean isMatch(String s, String p) {
int m=s.length(),n=p.length();
boolean[][] match = new boolean[m+1][n+1];
match[0][0] = true;
for(int i=0;i<n;i++){
if(p.charAt(i)=='*')
match[0][i+1]=match[0][i];
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(p.charAt(j)=='?'||p.charAt(j)==s.charAt(i))
match[i+1][j+1]=match[i][j];
else if(p.charAt(j)=='*')
match[i+1][j+1]=match[i+1][j]||match[i][j+1];
}
}
return match[m]
;
相关文章推荐
- LeetCode 44.Wildcard Matching题目解析
- [LeetCode]44. Wildcard Matching 深入浅出讲解和代码示例
- LeetCode Online Judge 题目C# 练习 - Wildcard Matching
- 【leetcode】第10题 Regular Expression Matching 题目+解析+代码
- leetcode Wildcard Matching
- 【leetcode】第77题 Combinations 题目+解析+JAVA代码
- [LeetCode] Wildcard Matching
- Leetcode 44:Wildcard Matching
- Leetcode 之Wildcard Matching(32)
- LeetCode-Wildcard Matching-通配符匹配-DP
- [Leetcode] 44. Wildcard Matching 解题报告
- Wildcard Matching_Leetcode_#44
- [leetcode]Wildcard Matching @ Python
- LeetCode Wildcard Matching DP/贪心
- 【leetcode】第55题 Jump Game 题目+解析+代码
- LeetCode "Wildcard Matching"
- 【leetcode】第78题 Subsets 题目+解析+JAVA代码
- Leetcode44 Wildcard Matching
- Leetcode Wildcard Matching
- LeetCode::Wildcard Matching