10.Regular Expression Matching (String; Back-Track,DP)
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
思路:如果下个字符是'*',那么可以重复当前字符0,1,2...次=>带回溯的递归。O(n)=kn,k是*出现的次数。
- class Solution {
- public:
- bool isMatch(string s, string p) {
- return dfsIsMatch(s,p,0,0);
- }
- bool dfsIsMatch(const string& s, const string& p, int sIndex, int pIndex){
- if (p[pIndex] == '\0') //结束条件:s若是'\0',p必须也是'\0'
- return s[sIndex] == '\0';
- if (p[pIndex+1] == '*') {
- /* '.' means any character (except '\0')
- * '.' means repeat 0 or more times
- * '.*' means repeat '.' 0 or more times
- */
- while ((s[sIndex] != '\0' && p[pIndex] == '.') || s[sIndex] == p[pIndex]) { //'.'可以与除'\0'以外的任何字符匹配
- if (dfsIsMatch(s, p, sIndex, pIndex+2)) //p[pIndex] repeat 0 times
- return true;
- sIndex += 1;//p[pIndex]在原基础上repeat次数+1
- }
- return dfsIsMatch(s, p, sIndex, pIndex+2); //when s[sIndex] != p[pIndex] && p[pIndex] != '.'(此时只有s[sIndex]==p[pIndex]=='\0'时可能return true)
- }
- else if ((s[sIndex] != '\0' && p[pIndex] == '.') || s[sIndex] == p[pIndex]) {
- return dfsIsMatch(s, p, sIndex + 1, pIndex + 1);
- }
- return false;
- }
- };
法II:动态规划。设二位数组dp[i][j]来表示两个string的状态关系,dp[i][j]=true,表示s[0..i-1]匹配p[0..j-1]的结果。O(n)=n2
dp的下标0表示NULL,设置的原因是,比方如果p的第二个字符是*,那么p可以从第三个字符开始与s的第一个字符匹配,也就是dp[0][2]=true。
- class Solution {
- public:
- bool isMatch(string s, string p) {
- int sLen=s.length();
- int pLen = p.length();
- bool dp[sLen+1][pLen+1]={false}; //[0]for string NULL
- //initialize
- dp[0][0]=true;
- for(int j = 1; j <= pLen; j++){
- dp[0][j]=j>=2 && dp[0][j-2] && p[j-1]=='*'; //* each other letter
- }
- //state transfer
- for(int i = 1; i <= sLen; i++){
- for(int j = 1; j <= pLen; j++){
- if(p[j-1]=='*'){
- dp[i][j]=(j>=2 && dp[i][j-2])/* repeat 0 times*/
- ||(dp[i][j-1])/*repeat 1 time*/
- ||(j>=2 && (s[i-1]==p[j-2] || p[j-2]=='.') && dp[i-1][j])/*repeat several times*/;
- }
- else{ //p[j-1]!='*'
- dp[i][j]=dp[i-1][j-1] && (s[i-1]==p[j-1] || p[j-1]=='.');
- }
- }
- }
- return dp[sLen][pLen];
- }
- };
10.Regular Expression Matching (String; Back-Track,DP)的更多相关文章
- 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(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- 刷题10. Regular Expression Matching
一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处 ...
- leetcode 10. Regular Expression Matching 、44. Wildcard Matching
10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...
- 《LeetBook》leetcode题解(10): Regular Expression Matching——DP解决正则匹配
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- leetcode problem 10 Regular Expression Matching(动态规划)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- [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 ...
随机推荐
- 执行Chrome自动化时--正在受到自动软件的控制的显示屏蔽
用selenium启动,浏览器出现‘Chrome正在受到自动软件的控制’ 屏蔽的方法: # coding:utf-8 from selenium import webdriver # 加启动配置 op ...
- 正则的使用及replace细讲
1.var reg=/./ 与 var reg=/\./的区别? .代表任意一个字符 \.而后者代表这个字符串中得有一个. 2.?的使用 如果单独的一个字符后面带? /\d?/ 代表1个或0个这个字符 ...
- [转载] ./configure,make,make install的作用
1.configure,这一步一般用来生成 Makefile,为下一步的编译做准备,你可以通过在 configure 后加上参数来对安装进行控制,比如代码:./configure –prefix=/u ...
- FDD vs TDD
双工方式 FDD vs TDD 频分双工(FDD) 收发信各占用一个频率. 优点是收.发信号同时进行,时延小,技术成熟,缺点是设备成本高. 时分双工(TDD) 收发信使用同一个频率,但使用不同 ...
- HTML5中常用的标签(及标签的属性和作用)
1.标签:<!DOCTYPE>作用:声明是文档中的第一成分,位于<html>标签之前. 2.标签:<html>作用:此元素可告知浏览器其自身是一个HTML文档.属性 ...
- 目前学习的爬取小数据图片zzz
import os import threading import re import time from lxml import etree all_img_urls = [] # 图片列表页面的数 ...
- pycharm最新版新建工程没导入本地包问题:module 'selenium.webdriver' has no attribute 'Firefox'
前言 最新版的pycharm做了很大的改变,新建工程的时候,默认不导入本地的安装包,这就导致很多小伙伴踩坑了... 明明已经pip安装过selenium了,但是却报AttributeError:mod ...
- ByteCache
private static class ByteCache { private ByteCache(){} //256个元素,用于缓存-128到127 static final Byte cache ...
- php获取服务器信息类
<?php/**+------------------------------------------------------------------------------* 获取服务器信 ...
- oracle跟踪sql语句
oracle跟踪sql语句 select * from v$sql 查询客户端电脑名称的ID select terminal, SID,SERIAL# from v$session where ( ...