【leetcode】10. Regular Expression Matching 简易正则匹配
1. 题目Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. The matching should cover the entire input string (not partial). The function prototype should be: Some examples: 2. 思路场景1:a*ab "aaab" 3. 代码耗时:79ms class Solution { public: bool isMatch(string s,string p) { return isMatch(s,p,0); } bool isMatch(const string& s,int i,const string& p,int j) { if (s.length() == i) { return p.length() == j || (p.length() > j+1 && p[j+1] == '*' && isMatch(s,i,j+2)); } if (p.length() > j+1 && p[j+1] == '*') { if (s[i] == p[j] || p[j] == '.') { return isMatch(s,i+1,j) || isMatch(s,j+2); } else { return isMatch(s,j+2); } } else { if (s[i] == p[j] || p[j] == '.') { return isMatch(s,j+1); } else { return false; } } } }; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |