LeetCode——010
来源:互联网 发布:魔兽光荣套淘宝卖多少 编辑:程序博客网 时间:2024/06/09 19:03
/* 题目: 10. Regular Expression Matching My Submissions QuestionEditorial SolutionTotal Accepted: 77160 Total Submissions: 352286 Difficulty: HardImplement 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", ".*") → true*//* 解题思路:需要用递归Recursion来解,大概思路如下:- 若p为空,若s也为空,返回true,反之返回false- 若p的长度为1,若s长度也为1,且相同或是p为'.'则返回true,反之返回false- 若p的第二个字符不为*,若此时s为空返回false,否则判断首字符是否匹配,且从各自的第二个字符开始调用递归函数匹配- 若p的第二个字符为*,若s不为空且字符匹配,调用递归函数匹配s和去掉前两个字符的p,若匹配返回true,否则s去掉首字母- 返回调用递归函数匹配s和去掉前两个字符的p的结果*/代码如下: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)); }};
0 0
- LeetCode——010
- 开始刷刷Leetcode—leetcode第二题
- LeetCode —— Combinations
- LeetCode —— Triangle
- LeetCode —— Anagrams
- [leetcode] Subsets——@
- Permutations——LeetCode
- minimum_window_substring ——leetcode
- LeetCode——Candy
- LeetCode——Triangle
- LeetCode——Combinations
- LeetCode——Permutations
- *candy——leetcode
- gas_station——leetcode
- LeetCode——Candy
- LeetCode——Triangle
- LeetCode——Anagrams
- LeetCode——Subsets
- 大端模式和小端模式
- table表单中的td内容两端对齐
- Git忽略规则及.gitignore规则不生效的解决办法
- Caffe CNN特征可视化
- android开发思路、一些想法
- LeetCode——010
- UITabBarController基础篇
- Android Button的隐藏与显示
- 迎接“物联云”,英特尔将推知识生成器工具套件
- 算数基本定理(唯一分解定理)
- 数据库事物隔离级别
- 将 Sublime 3 打造成 Python/Django IDE
- Linux socket编程之一:socket函数详细说明
- LeetCode 216. Combination Sum III