LeetCode 044 Wildcard Matching
2015-03-26 22:32
375 查看
题目
字符串匹配 “." 匹配任意字符串 “?”匹配单个字符串
代码
public class Solution {
public boolean isMatch(String s, String p) {
int m = s.length(), n = p.length();
int count = 0;
for (int i = 0; i < n; i++) {
if (p.charAt(i) == '*') count++;
}
if (count==0 && m != n) return false;
else if (n - count > m) return false;
boolean[] match = new boolean[m+1];
match[0] = true;
for (int i = 0; i < m; i++) {
match[i+1] = false;
}
for (int i = 0; i < n; i++) {
if (p.charAt(i) == '*') {
for (int j = 0; j < m; j++) {
match[j+1] = match[j] || match[j+1];
}
} else {
for (int j = m-1; j >= 0; j--) {
match[j+1] = (p.charAt(i) == '?' || p.charAt(i) == s.charAt(j)) && match[j];
}
match[0] = false;
}
}
return match[m];
}
}
思路
DP的字典记录。
record[j]代表 j和到第i个字符的字符串是否能够匹配。
字符串匹配 “." 匹配任意字符串 “?”匹配单个字符串
代码
public class Solution {
public boolean isMatch(String s, String p) {
int m = s.length(), n = p.length();
int count = 0;
for (int i = 0; i < n; i++) {
if (p.charAt(i) == '*') count++;
}
if (count==0 && m != n) return false;
else if (n - count > m) return false;
boolean[] match = new boolean[m+1];
match[0] = true;
for (int i = 0; i < m; i++) {
match[i+1] = false;
}
for (int i = 0; i < n; i++) {
if (p.charAt(i) == '*') {
for (int j = 0; j < m; j++) {
match[j+1] = match[j] || match[j+1];
}
} else {
for (int j = m-1; j >= 0; j--) {
match[j+1] = (p.charAt(i) == '?' || p.charAt(i) == s.charAt(j)) && match[j];
}
match[0] = false;
}
}
return match[m];
}
}
思路
DP的字典记录。
record[j]代表 j和到第i个字符的字符串是否能够匹配。
相关文章推荐
- [LeetCode]044-Wildcard Matching
- leetcode 044 —— Wildcard Matching
- Java for LeetCode 044 Wildcard Matching
- [LeetCode]题解(python):044-Wildcard Matching
- Leetcode -- Wildcard Matching
- LeetCode - Wildcard Matching
- [LeetCode] 44. Wildcard Matching
- LeetCode 44. Wildcard Matching
- [LeetCode] Wildcard Matching
- LeetCode--wildcard-matching
- Leetcode-44. Wildcard Matching
- [LeetCode]--44. Wildcard Matching
- leetcode(44) - Wildcard Matching
- Leetcode 44. Wildcard Matching
- leetcode 44. Wildcard Matching
- LeetCode : Wildcard Matching
- Leetcode_wildcard-matching(c++ and python version)
- leetcode - Wildcard Matching
- [LeetCode]Wildcard Matching
- Leetcode | Wildcard Matching