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:
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

思路: 这题 ugly 之处,在于 '*' 给的很不明朗, 其实'c*', 表示 '*' 可以代表 'ccc...cc'。
对 '*' 的理解,若出现 p中出现 '*', 则要比较用 s 和 p 中 '*' 之前的那个元素比较,从左往右找出 s 中第一个与 p 之前的元素不同的元素。
(若全相同,例如, 之前那个元素为 '.', 则 '.*' (表示'.......^....')可以匹配任何 s 串)。
//垃圾题!rubbish question!
class Solution {
public:
bool isMatch(const char *s, const char *p) {
if (s == NULL || p == NULL) return false;
if (*p == '\0') return *s == '\0';
// ".*" matches "", so we can't check (*s == '\0') here.
if (*(p + 1) == '*'){
// Here *p != '\0', so this condition equals with
// (*s != '\0' && (*p == '.' || *s == *p)).
while ((*s != '\0' && *p == '.') || *s == *p){
if (isMatch(s, p + 2)) return true;
++s;
}
return isMatch(s, p + 2);
}
else if ((*s != '\0' && *p == '.') || *s == *p){
return isMatch(s + 1, p + 1);
}
return false;
}
};

66. Regular Expression Matching的更多相关文章

  1. [LeetCode] Regular Expression Matching 正则表达式匹配

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  2. [LeetCode] 10. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...

  3. No.010:Regular Expression Matching

    问题: Implement regular expression matching with support for '.' and '*'.'.' Matches any single charac ...

  4. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  5. 【leetcode】Regular Expression Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...

  6. 【leetcode】Regular Expression Matching (hard) ★

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  7. 【JAVA、C++】LeetCode 010 Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  8. LeetCode | Regular Expression Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...

  9. [LeetCode] Regular Expression Matching(递归)

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

随机推荐

  1. IT公司100题-28-整数的二进制表示中1的个数

    问题描述: 输入一个整数n,求n的二进制表示中,一共有多少个1.例如n=8,二进制表示为00001000,二进制表示中有1个1.     分析: 如果一个数n不为0,那么n-1的二进制表示,与n的二进 ...

  2. 002 C#学前入门

    2016-01-16 1..Net平台  .Net FrameWork框架.Net FrameWork框架提供了一个稳定的运行环境,:来保障我们.Net平台正常的运转 2.C#语言 c sharp编程 ...

  3. 立即调用的函数表达式IIFE

    1.写法 (function () { alert("IIFE");})();//或者(function () { alert("IIFE"); }());

  4. Swift语言—常量、变量、类型赋值

    常量:常量在作用域内不能被赋值且不会发生改变,只读不写,每个常量都必须有唯一的名字和内存空间:关键字:let   变量:变量在作用区域内可以它的值可以被改变,可读可写可覆盖,每个常量都必须有唯一的名字 ...

  5. 两个小的java程序,用于练习java基本语法

    1.输入两个数,求其加减乘除.用窗口的形式呈现 import javax.swing.JOptionPane; public class JJCC { public static void main( ...

  6. Android Studio Reference local .aar files

    repositories { flatDir { dirs 'libs' }} dependencies { compile 'com.android.support:support-v4:22.2. ...

  7. windows在当前位置打开终端

    1) 在此文件夹窗口内空白区域右键单击(需要同时按住Shift),从菜单中选择"在此处打开命令行窗口"的项:2) 在此窗口地址栏里直接输入cmd,回车即可.

  8. 浏览器兼容 copyToClipboard("拷贝内容")

    function copyToClipboard(txt) { if (window.clipboardData) { window.clipboardData.clearData(); clipbo ...

  9. 期望DP

    BZOJ 1415 #include <iostream> #include <cstring> #include <algorithm> #include < ...

  10. 第三个Sprint冲刺第五天

    讨论地点:宿舍 讨论成员:邵家文.李新.朱浩龙.陈俊金 讨论问题:继续昨天的工作