44. Wildcard Matching 用c编译

来源:互联网 发布:亚马逊关键词怎么优化 编辑:程序博客网 时间:2024/05/22 06:22

44. Wildcard Matching


http://blog.csdn.net/sunao2002002/article/details/46352971


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") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "*") → trueisMatch("aa", "a*") → trueisMatch("ab", "?*") → trueisMatch("aab", "c*a*b") → false

题目要求实现模糊匹配,代码如下:

[cpp] view plain copy
 print?
  1. class Solution {  
  2. public:  
  3.     bool isMatch(const char *s, const char *p) {  
  4.         bool isStar = false;  
  5.         const char* s1, *p1;  
  6.         while(*s && (*p || isStar)){  
  7.             if((*s==*p) || (*p=='?')){  
  8.                 s++; p++;  
  9.             }else if(*p == '*'){  
  10.                 isStar = true;  
  11.                 p++;  
  12.                 if(*p=='\0'return true;  
  13.                 s1 = s;  
  14.                 p1 = p;  
  15.             }else{  
  16.                 if(!isStar) return false;  
  17.                 p =p1;  
  18.                 s = ++s1;  
  19.             }  
  20.         }  
  21.         if(*s=='\0'){  
  22.             while(*p=='*')p++;  
  23.             if(*p=='\0'return true;  
  24.   
  25.         }  
  26.         return false;  
  27.   
  28.   
  29.     }  
  30. };  



原创粉丝点击