【LeetCode】【C++】Wildcard Matching

题目

‘?’ 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

思路

此题和第十题 Regular Expression Matching极为相像,只不过这个题‘*’可代表任意字符串,而10题只能表示若干个前面字母。举例来说,此题中*可以表示a,ab,abdfefa等,也可以表示空。但10题中*不可以单独出现,其必须和它前面的字母共同构成一个表达式,比如说a*表示若干个a,也即a,aa,aaa,或空。

仍然采用DP,设置一个vectorre(n+1),存放第一个字符串截止到该位置的匹配情况,然后从第二个字符串开始遍历,遇到*要特殊处理,对于字符串2的每一位,都要从字符串1中去寻找是否可以匹配上的位置,并把相应位置标记为真。每一个位置的状态均由前一位置决定,也就是说前一位置不匹配,这一位置一定不能匹配。遍历结束后输出最后一个位置的匹配结果。

代码

class Solution {
public:
    bool isMatch(const char *s, const char *p) {
        int m = strlen(s);
        int n = strlen(p);
        if (n==0) return m==0;
        if (m>30000) return false;          //没有这句话有一个case:aaaaaa居多a的过不去。。。
        vector<bool> re(m+1,false);
        re[0]=true;                         //默认第零位为匹配,因为有可能p[0]==*
        for (int i=0;i<n;i++){
            if (p[i]==‘*‘){                 //判断是否为*,*需要特殊处理
                for (int k=0;k<m;k++){
                    re[k+1]=re[k+1]||re[k]; //如果已经为真,则继续为真;否则要取决于前一位。
                }
            }
            else{
                for (int j=m;j>0;j--){      //取决于前一位,并且要求这一位匹配
                    re[j]=re[j-1]&&(p[i]==s[j-1]||p[i]==‘?‘);
                }
            }
            re[0] = re[0]&&p[i]==‘*‘;       //如果该位置不为*,则第零位不可为真
        }
        return re[m];
    }
};

转载请注明出处:http://blog.csdn.net/monkeyduck

欢迎留言,关注

时间: 2024-11-09 21:54:00

【LeetCode】【C++】Wildcard Matching的相关文章

【leetcode刷题笔记】Wildcard Matching

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 p

【leetcode刷题笔记】Regular Expression Matching

Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

【leetcode刷题笔记】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

【leetcode刷题笔记】Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 题解: 设置两个变量:右边kepler和前向游标forward.如果当前kepeler所指的元素和

【leetcode刷题笔记】Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解:深度优先搜索.用resul

【leetcode刷题笔记】Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true, as t

【leetcode刷题笔记】Insertion Sort List

Sort a linked list using insertion sort. 题解:实现链表的插入排序. 要注意的地方就是,处理链表插入的时候尽量往当前游标的后面插入,而不要往前面插入,后者非常麻烦.所以每次利用kepeler.next.val和head.val比较大小,而不是kepeler.val和head.val比较大小,因为如果用后者,要把head指向的节点插入到kepeler指向的节点的前面,如果kepeler指向的节点是头结点,就更麻烦了. 代码如下: 1 /** 2 * Defi

【leetcode刷题笔记】Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 题解: 思路比较简单,每条直线都可以表示为y=kx+b,所以对于任意三点,如果它们共线,那么它们中任意两点的斜率都相等. 所以就遍历points数组,对其中的每一个元素计算它和位于它后面的数组元素的斜率并保存在一个hashmap中. 这个hashmap的键就是两点构成直线的斜率,值就是和当前元素po

【leetcode刷题笔记】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:以前做过的Spiral Matrix是给一个矩阵螺旋式的输出,这道题是给一个n,螺旋式的