Leetcode 44. Wildcard Matching
2017-05-11 00:34
453 查看
题目:
思路:
代码:
public class Solution {
public boolean isMatch(String s, String p) {
char[] chars = s.toCharArray();
char[] charp = p.toCharArray();
int ss = -1,pp = -1;
int sIndex = 0,pIndex = 0;
while(sIndex<chars.length){
if(pIndex == charp.length){//false,回溯
if(pp == -1) return false;
pIndex = pp+1; sIndex = ss++;
}
else if(charp[pIndex] == '?' || chars[sIndex] == charp[pIndex]){//相同
pIndex++;sIndex++;
}else if(charp[pIndex] == '*'){
pp = pIndex;ss = sIndex;pIndex = pp+1;
}else{
if(pp == -1) return false;
pIndex = pp+1;sIndex = ss++;
}
}
while(pIndex<charp.length){
if(charp[pIndex] != '*')
break;
pIndex++;
}
return pIndex == charp.length;
}
}
'?' 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
思路:
代码:
public class Solution {
public boolean isMatch(String s, String p) {
char[] chars = s.toCharArray();
char[] charp = p.toCharArray();
int ss = -1,pp = -1;
int sIndex = 0,pIndex = 0;
while(sIndex<chars.length){
if(pIndex == charp.length){//false,回溯
if(pp == -1) return false;
pIndex = pp+1; sIndex = ss++;
}
else if(charp[pIndex] == '?' || chars[sIndex] == charp[pIndex]){//相同
pIndex++;sIndex++;
}else if(charp[pIndex] == '*'){
pp = pIndex;ss = sIndex;pIndex = pp+1;
}else{
if(pp == -1) return false;
pIndex = pp+1;sIndex = ss++;
}
}
while(pIndex<charp.length){
if(charp[pIndex] != '*')
break;
pIndex++;
}
return pIndex == charp.length;
}
}
相关文章推荐
- LeetCode 44. Wildcard Matching
- LeetCode(44) Wildcard Matching
- Leetcode (44) Wildcard Matching
- LeetCode(44) Wildcard Matching
- leetcode 44. Wildcard Matching
- 【leetcode 44】Wildcard Matching
- LeetCode 44 Wildcard Matching
- LeetCode44. Wildcard Matching
- Leetcode:44. Wildcard Matching
- [*leetcode 44]. Wildcard Matching
- LeetCode44 Wildcard Matching
- leetcode 44. Wildcard Matching
- Leetcode习题解答:44. Wildcard Matching
- [leetcode 44] Wildcard Matching
- leetcode 44. Wildcard Matching
- LeetCode 44:Wildcard Matching
- leetcode 44. Wildcard Matching
- LeetCode 44. Wildcard Matching
- leetcode 44. Wildcard Matching
- [leetcode[ 【字符串】 44. Wildcard Matching