LeetCode OJ--Regular Expression Matching
http://oj.leetcode.com/problems/regular-expression-matching/
问题给想复杂了,只有p中可以含有. *,s中的字符都是确定的。想了好久,最终还是参考了网上的答案。等我再想想。
#include <iostream>
#include <map>
#include <string>
using namespace std; class Solution {
public:
bool isMatch(const char *s, const char *p)
{
if (s == NULL || p == NULL) return false;
if (*p == '\0') return *s == '\0'; if (*(p + ) == '*')
{
while ((*s != '\0' && *p == '.') || *s == *p)
{
if (isMatch(s, p + )) return true; //aab a*cd
++s;
} return isMatch(s, p + ); //aab c*ab 的情况
}
else if ((*s != '\0' && *p == '.') || *s == *p)
{
return isMatch(s + , p + );
} return false;
}
}; int main()
{
Solution myS;
char *s = "aab";
char *p = "a*cd";
myS.isMatch(s,p);
return ;
}
于是,又试图用自己的解法,来做这道题。
class Solution {
public:
bool isMatch(const char *s, const char *p)
{
if (s == NULL || p == NULL) return false;
int j = ,i = ;
char flagchar = '\0';
while(s[i]!='\0'&&p[j]!='\0')
{
if(s[i] == p[j] )
{
flagchar = s[i];
i++;
j++;
}
else if(p[j] == '.')
{
i++;
j++;
flagchar = '\0';
}
else if(s[i]!= p[j] && p[j]!= '*' && p[j] != '\0' && p[j+]!='\0' && p[j+] == '*')
{
flagchar = p[j];
j+= ;
}
else if(p[j]=='*')
{
if(flagchar == '\0')
flagchar = s[i];
while(p[j+]!='\0'&&p[j+]==flagchar)
j++;
j++;
while(s[i]!='\0'&&s[i]==flagchar)
i++;
}
else
break;
}
if(s[i]=='\0'&&p[j]=='\0')
return true; return false;
}
};
但是在"aaa", "ab*a*c*a"里,挂掉了。发现,当该边的只是规模的时候,确实递归非常好用。在这个地方,确实得递归。
在多种情况匹配不确定的时候,这个尝试
while ((*s != '\0' && *p == '.') || *s == *p)
{
if (isMatch(s, p + 2)) return true; //aab a*cd
++s;
}
return isMatch(s, p + 2); //aab c*ab 的情况
这里,大赞啊!
又长智商了。
LeetCode OJ--Regular Expression Matching的更多相关文章
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- [LeetCode][Python]Regular Expression Matching
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...
- LeetCode (10): Regular Expression Matching [HARD]
https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...
- [LeetCode] 10. Regular Expression Matching 正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- Leetcode 10. Regular Expression Matching(递归,dp)
10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...
- [LeetCode] 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...
- 【leetcode】Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
- 【leetcode】Regular Expression Matching (hard) ★
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【JAVA、C++】LeetCode 010 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- Java [leetcode 10] Regular Expression Matching
问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
随机推荐
- 【贪心】bzoj1577: [Usaco2009 Feb]庙会捷运Fair Shuttle
一类经典的线段贪心 Description 公交车一共经过N(1<=N<=20000)个站点,从站点1一直驶到站点N.K(1<=K<=50000)群奶牛希望搭乘这辆公交车.第i ...
- linux :没有找到 ifconfig netstat
linux :没有找到 ifconfig netstat ubuntu sudo apt install net-tools -y centos yum install net-tools
- CentOS 7.4 基于LNMP搭建wordpress
之前有好多次搭建wordpress的经历,有在Ubuntu系统上,有在CentOS7.2系统上,但都是搭完还是稀里糊涂的,因为好多都是教程上照着敲的.这次好好出个教程,以便以后方便查看. 准备工作:C ...
- LeetCode(307) Range Sum Query - Mutable
题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...
- LeetCode(242)Valid Anagram
题目 Given two strings s and t, write a function to determine if t is an anagram of s. For example, s ...
- hdu 5533
Dancing Stars on Me Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
- Cinder配置多Ceph后端步骤
1. 检查cinder当前backend配置 使用cinder service-list,查看cinder-volume服务的Host字段格式. 旧版格式: 新版格式: 旧版中Host字段是cinde ...
- 天气API接口的使用
最近项目中使用到了天气预报的功能,需要从网上获取天气数据,然后显示在公司系统的页面上. 在这里和大家分享下我的做法,希望能对大家有所帮助,如果有错误,欢迎大家指正. 先给大家看看效果: 下面开始进行讲 ...
- TTL与COMS的区别
1.电平的上限和下限定义不一样,CMOS具有更大的抗噪区域. 同是5伏供电的话,ttl一般是1.7V和3.5V的样子,CMOS一般是 2.2V,2.9V的样子,不准确,仅供参考. 2.电流驱动能力不 ...
- [python] 求大神解释下 面向对象中方法和属性
面向对象中 类方法 实例方法 类属性 实例属性该如何理解呢?