[leetcode] Regular Expression Matching
来源:互联网 发布:百变气泡软件 编辑:程序博客网 时间:2024/05/06 07:54
from : https://leetcode.com/problems/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") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "a*") → trueisMatch("aa", ".*") → trueisMatch("ab", ".*") → trueisMatch("aab", "c*a*b") → true
public class Solution { public boolean isMatch(String s, String p) {if (null == s || null == p) {return s == p;}int lenS = s.length();int lenP = p.length();if (0 == lenP) {return 0 == lenS;}if (1 == lenP) {if (p.equals(s) || ".".equals(p) && 1 == s.length()) {return true;} else {return false;}}if ('*' != p.charAt(1)) {if (0 < s.length() && (p.charAt(0) == s.charAt(0) || '.' == p.charAt(0))) {return isMatch(s.substring(1), p.substring(1));}return false;} else {while (0 < s.length() && (p.charAt(0) == s.charAt(0) || p.charAt(0) == '.')) {if (isMatch(s, p.substring(2))){return true;}s = s.substring(1);}return isMatch(s, p.substring(2));}}}
class Solution {public: bool isMatch(string s, string p) { int ls = s.size(), lp = p.size(); if(0 == lp) return 0 == ls; if(1 == lp) { return 1 == ls && ('.' == p[0] || p[0] == s[0]); } if('*' != p[1]) { if(0 < ls && (s[0] == p[0] || '.' == p[0])) { return isMatch(s.substr(1), p.substr(1)); } return false; } else { while(0 < s.size() && (s[0] == p[0] || '.' == p[0])) { if(isMatch(s, p.substr(2))) { return true; } s = s.substr(1); } return isMatch(s, p.substr(2)); } }};
0 0
- 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
- [LeetCode] 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
- leetcode:Regular Expression Matching
- Java虚拟机 运行时数据区
- OC学习 -- NSDate和NSDateFormatter
- Java学习--(七)类
- 指数型母函数理解
- hdu 1847 博弈
- [leetcode] Regular Expression Matching
- iOS学习:调用相机,选择图片上传,带预览功能
- 【玩玩飞凌OK6410-B】序列之13:QT和屏幕校准测试
- Linux文件系统
- HDU 2073 无限的路
- OC学习 -- Extension
- 【MySql】使用记录<二>
- 源码管理工具的使用
- Android-ListView优化常见的三种方式