LeetCode 10
2018-03-16 21:47
246 查看
LeetCode 10
Regular Expression Matching
Reference:http://www.cnblogs.com/grandyang/p/4461713.htmlProblem Description:
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).
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:
没嘛好说,gg,看别人的也只能勉强理解,自己想不到比较有条理的思路。编译原理白学了==
class Solution { public: bool isMatch(string s, string p) { if (p.empty()) return s.empty(); if (p.size() == 1) { return (s.size() == 1 && (s[0] == p[0] || p[0] == '.')); } if (p[1] != '*') { if (s.empty()) return false; return (s[0] == p[0] || p[0] == '.') && isMatch(s.substr(1), p.substr(1)); } while (!s.empty() && (s[0] == p[0] || p[0] == '.')) { if (isMatch(s, p.substr(2))) return true; s = s.substr(1); } return isMatch(s, p.substr(2)); } };
相关文章推荐
- leetcode10 Regular Expression Matching @python
- leetcode之旅(10)-Roman to Integer
- Leetcode_10
- LeetCode 10 Regular Expression Matching
- LeetCode 10:《Regular Expression Matching》
- [LeetCode-168] Excel Sheet Column Title(10进制转26进制)
- LeetCode: 继续easy题10
- leetcode 23:Merge k Sorted Lists(15-10-10)
- 面试笔试杂项积累-leetcode 6-10
- LeetCode 9~10
- leetcode 10 -- Regular Expression Matching
- [*leetcode 10] Regular Expression Matching
- LeetCode 10 Regular Expression Matching
- leetcode经典编程题(10)
- [LeetCode] 10. Regular Expression Matching java
- leetcode之旅(10)-Roman to Integer
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
- 10_leetcode_Valid Parentheses
- leetcode题解c++ | 10. Regular Expression Matching
- LeetCode 10: Regular Expression Matching