LeetCode 10 Regular Expression Match
'.' 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
对于我这种算法菜鸟都不算的门外汉,自然写出了又长又臭的if else考虑了一大堆的case,结果还是各种bug各种case通不过。。。醉了,早好好学习也不至于现在这么狼狈,还好不是太晚。
最终参考了http://blog.csdn.net/hopeztm/article/details/7992253浙大牛人的思路加代码
这题主要运用动态规划(DP):(这里用s指向源串,p指向待匹配串)
跟上题回文匹配类似使用dp[i][j]表示s[0...strlen(s)]和p[0...strlen(p)]的匹配情况
if *(p+1) != '*'
if *p == *s dp[i][j] = dp[i+1][j+1]
else return false
if *(p+1) == '*' 这是需要考虑拓展.*的情况来匹配,并且每一步都要增加s,如果匹配返回true,否则返回通配符匹配后的结果。
这里面由dp[i][j]到dp[i+1][j+1]就是迭代的过程,后面每次增加s检查匹配也是迭代,若增加s的过程无匹配,则退回到最开始通配符匹配环节。
LeetCode 10 Regular Expression Match的更多相关文章
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- 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 、44. Wildcard Matching
10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...
- [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 [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正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- [LeetCode] 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...
- Java [leetcode 10] Regular Expression Matching
问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
随机推荐
- IDEA的terminal设置成Linux的终端一样
方式一:通过在Windows上安装Linux命令行工具 前提:需要安装Linux终端的命令行工具,并且最好可以安装 Gow (一个Windows下模拟Linux命令行工具集合,它集成了 Liunx 环 ...
- 在Kotlin上怎样用Mockito2 mock final 类(KAD 23)
作者:Antonio Leiva 时间:Mar 2, 2017 原文链接:https://antonioleiva.com/mockito-2-kotlin/ 如我们在前面文章中谈到的,Kotlin最 ...
- 感知机学习算法(PLA)
Perception Learning Algorithm, PLA 1.感知机 感知机是一种线性分类模型,属于判别模型. 感知机模型给出了由输入空间到输出空间的映射: f(X) = sign(WTX ...
- 【集训试题】exam 信心考 最小割
题意概述: 有N个人,A,B两个考场.如果学生i在A考场,总信心值增加xi:如果学生i在B考场,总信心值增加yi.其中还有m对好友,当第i对好友的两个人都在A考场时,总信心值增加ai:如果两人都在B考 ...
- python学习笔记-list的用法
1.list的定义 list = [] list = [1,2,'a','b'](list中的元素不一定是一个类型) 2.list的操作 1)list.append(value) 2)list.ins ...
- ssh问题_1
昨天配置了虚拟机的ssh,可以相互连接,但是今天早上就不行了,遇到如下错误 [slave1@hadoop ~]$ scp .ssh/id_rsa.pub master@hadoop.master:~/ ...
- http请求整理
终于又回来了,先来简单整理一波http请求的信息.对于前端来说,不管是在面试还是在实际项目中,都有必要去了解一些关于http的信息. http请求包含三部分:请求行request line.请求头re ...
- 使用XML传递数据
HTML <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...
- Phaser的timer用法
1. 延迟timer,相当于setTimeout game.time.events.add(Phaser.Timer.SECOND*5,this.delayOver,this); 2. 循环timer ...
- vector 搜索
http://classfoo.com/ccby/article/cIBahI #include <iostream> #include <algorithm> #includ ...