Wildcard Matching

迭代:


#include<iostream>
using namespace std;
class Solution
{
public: bool isMatch(const char *s, const char *p)
        {
            int i=0,j=0;
            bool str=false;
            for(;s[i]!='\0';i++,j++)
            {
                switch (p[j])
                {
                case '?':   
                    break;
                case '*':
                    while(p[j]=='*')
                        j++;
                    if(p[j]=='\0') 
                        return true;
                    str=true;
                    i--;
                    j--;
                    break;
                default:
                    if(s[i]!=p[j])
                    {   
                        if(!str)
                            return false;
                        else
                            j--;
                            break;
                    }
                }
            }
            while(p[j++]=='*');
            return p[j]=='\0';
        }
};

void main()
{
    Solution solution;
    cout<<solution.isMatch("aab","*a*b");
}
發佈了51 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章