`
SunnyYoona
  • 浏览: 366832 次
社区版块
存档分类
最新评论

[LeetCode]10.Regular Expression Matching

 
阅读更多

题目

mplement 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:
bool isMatch(const char *s, const char *p)

Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true

思路

这里写图片描述

代码

/*------------------------------------------------------------------------------------
*   日期:2014-04-03
*   作者:SJF0115
*   题目: 10.Regular Expression Matching
*   来源:http://oj.leetcode.com/problems/regular-expression-matching/
*   结果:AC
*   来源:LeetCode
------------------------------------------------------------------------------------*/
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

class Solution {
public:
    bool isMatch(const char *s, const char *p) {
        if(s == NULL || p == NULL || *p == '*') {
            return false;
        }
        if(*p == '\0') return *s == '\0';
        //next char is not '*': must match current character
        if(*(p+1) != '*') {
            if(*s == '\0') return false;
            if(*p != '.' && *p != *s) return false;
            return isMatch(s+1,p+1);
        }
        //next char is '*'
        else {
            int slen = strlen(s);
            if(isMatch(s,p+2)) return true;
            for(int i = 0; i < slen; ++i) {
                if(*p!='.' && *p != *(s+i)) return false;
                if(isMatch(s+i+1,p+2)) return true;
            }
            return false;
        }
    }
};

int main() {
    Solution solution;
    char* s = "abcbcd";
    char* p = "ab*bbc";
    bool result = solution.isMatch(s,p);
    cout<<result<<endl;
    return 0;
}
<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>
分享到:
评论

相关推荐

    leetcode338-LeetCode:LeetCode刷题总结

    10.Regular Expression Matching 11.Container With Most Water 12.Integer to Roman 13.Roman to Integer 14.Longest Common Prefix (Trie树待完成) 15.3Sum 16.3Sum Closest 17.Letter Combinations of a Phone ...

    leetcodepython001-algorithm:leetcode问题(cpp、java、python),书籍破解_the_coding

    leetcode Python 001 leetcode的算法问题 这是我的解决方案,用 ...Expression Matching 011. Container With Most Water 012. Integer to Roman 013. Roman to Integer 014. Longest Common Prefix 019. R

    lrucacheleetcode-leetcode-1:leetcode-1

    lru cache leetcode Leetcode ...Expression Matching 动态规划,列出转换方程即可,注意初值 记T[i][j] = 是否S[0:i]和P[0:j]匹配 再分类讨论,其中pattern *分为0, 1, more三种类型 0: i不变, j+1

    lrucacheleetcode-luoleet:LeetcodesolutionsbyXinhangLuoandQinghaoDai

    https://leetcode.com/problems/regular-expression-matching/ Regular Expression Matching 100 https://leetcode.com/problems/same-tree/ Same Tree 101 https://leetcode.com/problems/symmetric-tree/ ...

    leetcode跳跃-LeCode:乐科

    leetcode 跳跃 LeetCode Solved by ...Expression Matching 正则表达式匹配 11. Container With Most Water 盛最多水的容器 12. Integer to Roman 整数转罗马数字 13. Roman to Integer 罗马数字转

    leetcode数组下标大于间距-leetcode:leetcode刷一遍

    leetcode数组下标大于间距 5. Longest Palindromic Substring 这里使用menecher方法,就是动态规划,首先在原先的字符串之间插入'#, 这样可以统一处理奇数串和偶数串, 使用两个变量纪录状态, far_pos表示最长回文...

    leetcode怎么销号-leetcodeAns:leetcode问题的答案python

    10.Regular Expression Matching: 递归的方法:当前正则第二个字符不为'*',很简单,比较当前,两个指针都往右移动即可,继续这样比较。如果为空,则有两种方式,第一种是正则的指针往后移动,字符串的保持不变,另一...

    leetcode写题闪退-LeetCode:leetcodeOJ

    2014.10.29 对于Find Minimum in Rotated Sorted Array II 和 Find Minimum in Rotated Sorted Array这两道题目,我也是醉了。。。代码完全一样的不说,题目还都特别傻逼。 Maximum Product Subarray 动态规划,类似...

    Coding Interview In Java

    10 Regular Expression Matching in Java 39 11 Merge Intervals 43 12 Insert Interval 45 13 Two Sum 47 14 Two Sum II Input array is sorted 49 15 Two Sum III Data structure design 51 16 3Sum 53 17 4Sum 55...

    leetcode中国-leetcode:leetcode刷题

    Expression Matching 递归匹配 wildcard matching 动态规划 longest common prefix , 简单 valid number, hard, 用有限自动机 integer to roman ,easy , 模拟 roman to integer ,easy , 模拟 count and say , easy ,...

    分割数组求最大差值leetcode-Leetcode-Road:LeetCode刷题记录

    分割数组求最大差值leetcode LeetCode 学习之路 记录自己完成LeetCode的代码和结果。 序号 中文名称 英文名称 通过率 难度 1 Two Sum 47.0% 简单 2 Add Two Numbers 36.0% 中等 3 Longest Substring Without ...

    lrucacheleetcode-LeetCode:我从LeetCode提交的一些任务

    (/problems/regular-expression-matching/) 24.1% 难253  38.9% 中15 21.6% 中277 寻找名人 (/problems/find-the-celebrity/)  35.4% 中等158 读取 N 个字符给定 Read4 II - 多次调用 (/problems/read-n-...

    LeetCode-in-Golang:使用 Golang 解答 LeetCode

    35%2☆ ☆27%3☆ ☆24%4☆ ☆ ☆21%5☆ ☆25%6☆ ☆26%7☆24%8☆ ☆13%9Palindrome Number☆35%10Regular Expression Matching☆ ☆ ☆24%:red_heart:11Container With Most Water☆ ☆36%12Integer to R

    leetcode530-algorithm:算法

    Expression Matching 011 Container With Most Water 012 Integer to Roman 013 Roman to Integer 014 Longest Common Prefix 015 3Sum 016 3Sum Closest 017 Letter Combinations of a Phone Number 018 4Sum 020 ...

    leetcode添加元素使和等于-leetcode:我的leetcode解决方案

    leetcode添加元素使和等于 经验教训 一定要吧功能尽量细化为函数,这样一者做题思路比较清晰,二者可以在某些情况下直接return值。 如果输入的形式是一个序列,则可以想想:分治、动规、贪婪,一般不建议搜索,因为...

    leetcode答案-leetcode:leetcode

    leetcode 答案 leet code 这是我的leetcode答案,语言主要是用python。很多算法不是最优,甚至很槽糕,有空就会优化 目前完成了: Two Sum Add Two Numbers Longest ...Regular Expression Matching

    lrucacheleetcode-LeetCode_Note:leetcode个人笔记

    [10_regular-expression-matching.cpp] [100_same-tree.cpp] [1001_sorted-merge-lcci.cpp] [101_对称树.cpp] [102_binary-tree-level-order-traversal.cpp] [103_binary-tree-zigzag-level-order-traversal.cpp] ...

    leetcode正则表达式-DP-7:DP-7

    leetcode正则表达式 DP-7 Problem1 Edit Distance () Problem2 Regular Expression Matching ()

    LeetCode_Note:刷LeetCode题解及笔记

    LeetCode 自己刷LeetCode整理的一些题解笔记,有什么错误欢迎指出。 带:club_suit:为力扣2020.3.1开始的每日一题打卡 ...Regular-Expression-Matching C++, Python Hard 11:club_suit: Container-With-

    lrucacheleetcode-leetcode:leetcodesolutionsinC++微信公众号:曲奇泡芙(互联网&智能汽车技术)

    lru cache leetcode leetcode leetcode solutions in C++ 微信公众号:曲奇泡芙 (互联网&车联网技术) ..../0010-regular-expression-matching.cpp ./0011-container-with-most-water.cpp ./0012-int

Global site tag (gtag.js) - Google Analytics