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 ...
随机推荐
- 关于bc中小数点length,scale,(())以及进制转换
这是我在codewar上遇到的一个题,我用我自己的方法做出了解答,如下: 1 #!/bin/bash 2 3 distance=`echo "$1*10000"|bc|cut -d ...
- Python3 简单封装 sqlite3 - SimpleToolSql
#coding: utf-8 #Author:boxker #Mail:icjb@foxmail.com import sqlite3 import os class simpleToolSql(): ...
- centos7.4系统部署nodejs前端项目
1.安装nodejs运行环境 wget命令下载Node.js安装包,该安装包是编译好的文件,解压之后,在bin文件夹中就已存在node和npm,无需重复编译 wget https://nodejs.o ...
- python输出mssql 查询结果示例
# -*- coding: utf-8 -*-# python 3.6import pymssql conn=pymssql.connect(host='*****',user='******',pa ...
- 利用for循环和range输出9 * 9乘法口诀表
li = [2, 3, 4, 5, 6, 7, 8, 9, 10] for i in li: for j in range(1, i): print('{0} * {1} = {2}'.format( ...
- ACM 广度优化搜索算法总结
广度优化搜索算法的本质:要求每个状态不能重复,这就需要我们:第一次先走一步可以到达的状态,如果还没有找到答案,就需要我们走到两步可以到达的状态.依次下去 核心算法:队列 基本步骤: ...
- Linux对大容量硬盘分区
随着单块硬盘容量的增大和硬盘价格的下降,2TB的磁盘使用将很快会普及,由于传统的MBR方式存储分区表的方 式缺陷,将可能导致很多分区工具不能正确地读取大于2TB容量的硬盘而无法正常分区大容量硬盘.其实 ...
- Mybatis(1、核心配置文件、Properties、Settings、typeAliases...)
Mybatis(1.核心配置文件.Properties.Settings.typeAliases...) 2017年04月23日 22:52:36 阅读数:1527 此章主要介绍sqlMapConfi ...
- jquery获得iframe内容的高度
html: <iframe name="rightgp" id="right_frame_h" src="/Poster/rightgp&quo ...
- sgen.exe 未能运行
指定的任务可执行文件“sgen.exe”未能运行.文件名或扩展名太长. 解决方式,右键项目属性->生成 把下图红框的“开”设置成“自动”