博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 10: Regular Expression Matching
阅读量:4150 次
发布时间:2019-05-25

本文共 882 字,大约阅读时间需要 2 分钟。

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

代码如下

class Solution {public:    bool isMatch(const char *s, const char *p) {     	if(*p == '\0')		return *s == '\0';	if(*(p+1) != '*'){		if((*p==*s) || ((*p=='.') && (*s!='\0')))                          return isMatch(s+1, p+1);		else 			return false;	}else{		while(*s && (*s==*p || *p=='.'))		{			if(isMatch(s, p+2))				return true;			s++;		}		return isMatch(s, p+2);	}	return false;    }};

转载地址:http://cbxti.baihongyu.com/

你可能感兴趣的文章
getopts用法
查看>>
关于char *ptr;
查看>>
该死的IFS变量
查看>>
ls的详解
查看>>
例析#define
查看>>
C写的实现wc和ls函数
查看>>
一个汉字真的由两个字节存放吗?
查看>>
查看ubuntu的温度
查看>>
ubuntu下j在安装ava环境
查看>>
使用优盘装系统
查看>>
整型数据和字符串数据相互转化代码
查看>>
有趣的随机数
查看>>
linux下常用linux c函数
查看>>
2009-2010寒假php学习笔记
查看>>
c语言中的 '\0'
查看>>
php中的单引号和双引号的区别
查看>>
ubuntu解压命令
查看>>
ubuntu快捷键
查看>>
int const *p 和 const int *p
查看>>
fedora安转nvidia显卡驱动后黑屏解决
查看>>