1 题目:

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

2 思路:

好吧,这题我开始一个一个比较真是跪了,没有用递归,考虑各种情况,各种if-else,发现还是无法考虑所有情况。看别人写的,使用递归写的,思路很清楚。

https://leetcode.com/discuss/32424/clean-java-solution

看那个c++的,想转为java,真是跪了,c++用'\0'表示字符串结束,而java字符串是数组,没有这个一说,各种越界加超时。

https://leetcode.com/discuss/9405/the-shortest-ac-code

3 代码:

    public boolean isMatch(String s, String p) {
if (p.isEmpty()) {
return s.isEmpty();
} if (p.length() == 1 || p.charAt(1) != '*') {
if (s.isEmpty() || (p.charAt(0) != '.' && p.charAt(0) != s.charAt(0))) {
return false;
} else {
return isMatch(s.substring(1), p.substring(1));
}
} //P.length() >=2 && p.charAt(1) == '*'
// the first char is match
while (!s.isEmpty() && (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.')) {
if (isMatch(s, p.substring(2))) {
return true;
}
//see if the next char of s is still match
s = s.substring(1);
}
//first char not match , make * to 0
return isMatch(s, p.substring(2));
}
												

[eetcode 10]Regular Expression Matching的更多相关文章

  1. leetcode 10 Regular Expression Matching(简单正则表达式匹配)

    最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...

  2. leetcode 10. Regular Expression Matching 、44. Wildcard Matching

    10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...

  3. Leetcode 10. Regular Expression Matching(递归,dp)

    10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...

  4. 刷题10. Regular Expression Matching

    一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处 ...

  5. leetcode problem 10 Regular Expression Matching(动态规划)

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

  6. 10. Regular Expression Matching

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

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

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

  8. [LeetCode] 10. Regular Expression Matching

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

  9. 【leetcode】10.Regular Expression Matching

    题目描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...

随机推荐

  1. NC 5系自定义显示公式

    1.继承NcInnerFunction(nc.vo.pub.formulaset.function.NcInnerFunction) 在方法中引用父类方法function,并在里面写方法 @Overr ...

  2. Photoshop零基础教程集锦,助你快速进阶为大佬,轻松、任性!!!

    现今,对于Web或App UI设计师而言,除了不断学习专业知识,提升设计技能.掌握一款得心应手的设计工具(例如设计师们常用的图像处理工具PhotoShop,矢量图绘制工具AI, 图形视频处理工具AE, ...

  3. centos 6.5 搭建zookeeper集群

    为什么使用Zookeeper? 大部分分布式应用需要一个主控.协调器或控制器来管理物理分布的子进程(如资源.任务分配等)目前,大部分应用需要开发私有的协调程序,缺乏一个通用的机制协调程序的反复编写浪费 ...

  4. Viewer.js 是一款强大的 jQuery 图像浏览插件。

    https://blog.csdn.net/qq_29132907/article/details/80136023 一.效果图  二.代码<!DOCTYPE html><html ...

  5. linux_制作本地yum源

    新建文件夹 mkdir /root/guazai 并将本地磁盘挂载到当前文件夹 mount -o loop /dev/cdrom /root/guazai/ 新建文件夹 mkdir /mnt/loca ...

  6. Hibernate 的Configuration、sessionFactory和session和transaction对象解释

    1.Configuration对象: Configuration conf=new Configuration(); conf.configure(); 1.1 到 src下面找到名称hibernat ...

  7. Lecture 5

  8. 2019.01.10 bzoj1095: [ZJOI2007]Hide 捉迷藏(动态点分治)

    传送门 蒟蒻真正意义上做的第一道动态点分治! 题意:给一棵最开始所有点都是黑点的树,支持把点的颜色变成从黑/白色变成白/黑色,问当前状态树上两个最远黑点的距离. 思路: 首先考虑不带修改一次点分治怎么 ...

  9. 2018.10.27 codeforces402D. Upgrading Array(数论+贪心)

    传送门 唉我觉得这题数据范围1e5都能做啊... 居然只出了2000 考完听zxyzxyzxy说我的贪心可以卡但过了? 可能今天本来是0+10+00+10+00+10+0只是运气好T1T1T1骗了10 ...

  10. 1-10假期训练(hdu-2059 简单dp)

    题目一:传送门 思路:水题,模拟即可 题目二:传送门 思路:dp,决策每个充电站是否要充电.(决策只有搜索,DP两种解决方法) (1)考虑状态的个数,n+2个,因为除了n个还有位置0,终点len两种状 ...