[LeetCode]010-Regular Expression Matching
来源:互联网 发布:java 异或 php 编辑:程序博客网 时间:2024/05/22 05:31
题目:
Regular Expression Matching
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
Solution:
//’.’很好匹配,’*’比较难匹配,采用递归算法
//(1)如果当前匹配的字符的后一位不是’*’,则直接对应匹配即可。(要考虑’.’)
//(2)如果当前匹配的字符的后一位是’*’,则分两步,一步是s不停后移与当前的p比较,直到出现不一样字符。一步是上一步结束后,将新的s与p+2(后移两位)开始重新比较
bool isMatch(string s, string p) { if(p.size() == 0) return s.size() == 0?true:false; if(p.size() == 1) { if(s.size() == 1 && (p[0]=='.' || p[0] == s[0])) return true; else return false; } if(p[1] != '*') { if(s.size()>0 && (p[0] == '.' || p[0] == s[0])) { return isMatch(s.substr(1),p.substr(1)); } else return false; } else { while(s.size() >0 && (s[0] == p[0] || p[0] == '.')) { if(isMatch(s,p.substr(2)))//aaa和a*a return true; s = s.substr(1); } return isMatch(s,p.substr(2)); } }
- leetcode-010:Regular Expression Matching
- LeetCode 010 Regular Expression Matching
- LeetCode 010-Regular Expression Matching
- [LeetCode]-010-Regular Expression Matching
- [LeetCode]010-Regular Expression Matching
- LeetCode 010 Regular Expression Matching
- leetcode--010 Regular Expression Matching
- LeetCode-010 Regular Expression Matching
- LeetCode: Regular Expression Matching
- LeetCode: Regular Expression Matching
- leetcode Regular Expression Matching
- [Leetcode] Regular Expression Matching
- [LeetCode]Regular Expression Matching
- LeetCode-Regular Expression Matching
- Regular Expression Matching LeetCode
- LeetCode | Regular Expression Matching
- LeetCode: Regular Expression Matching
- Leetcode: Regular Expression Matching
- HDOJ 5533-Hidden String
- hdoj--5333--Dancing Stars on Me(水题)
- Repair Windows BCD
- 把“后悔”当作“玩笑”
- 剑指offer系列之十一:数值的整数次方
- [LeetCode]010-Regular Expression Matching
- LayoutInflater源码分析
- 解决CocoaPods各种慢的方案(gem换源+pod repo换源)
- MyBatis的association示例以及association的两种形式
- 我的电脑的ADT软件解决Fail to create the java Virtual Machine问题
- java多线程 callable和future学习
- 计算机技术领域当前的主流技术及其社会需求调查报告
- 黑马程序员—— OC加强---ARC+Category+Block
- 限制文本框只能输入整数