LeetCode 010-Regular Expression Matching
来源:互联网 发布:linux 添加输入法 编辑:程序博客网 时间:2024/05/22 09:04
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
难度:难代码:该题要理清思路 从p的length入手,分情况讨论
public class Solution { public boolean isMatch(String s, String p) {<span style="white-space:pre"></span>if (p.length() == 0)<span style="white-space:pre"></span>return s.length() == 0;<span style="white-space:pre"></span>if (s.length() == 0) {<span style="white-space:pre"></span>if (p.length() == 1)<span style="white-space:pre"></span>return false;<span style="white-space:pre"></span>if (p.charAt(1) == '*')<span style="white-space:pre"></span>return isMatch(s, p.substring(2));<span style="white-space:pre"></span>return false;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>if (p.length() == 1) {<span style="white-space:pre"></span>if (p.charAt(0) == '.' && s.length() == 1)<span style="white-space:pre"></span>return true;<span style="white-space:pre"></span>return s.equals(p);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>if (p.length() >= 2 && p.charAt(1) != '*') {<span style="white-space:pre"></span>// if p(1) is not *, we need p(0) equals to s(0) or p(0) equals '.'<span style="white-space:pre"></span>if (p.charAt(0) == s.charAt(0) || p.charAt(0) == '.'<span style="white-space:pre"></span>&& s.length() != 0)<span style="white-space:pre"></span>// check if the left is also match<span style="white-space:pre"></span>return isMatch(s.substring(1), p.substring(1));<span style="white-space:pre"></span>return false;<span style="white-space:pre"></span>} else {<span style="white-space:pre"></span>while (s.length() > 0 && (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));}<span style="white-space:pre"></span>}}
0 0
- 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
- ubuntu14.04安装配置VTK-6.2.0
- 4-2 RFID系统二进制树型搜索算法是如何解决碰撞的?简述其实现步骤
- (17)sql注入与sql mode
- LeetCode 27 Remove Element
- 【RFID第五次作业】第八章课后习题
- LeetCode 010-Regular Expression Matching
- UIView 动画效果的四种调用方式
- java抽象类和接口的区别,使用场景以及设计思想分析
- Linux常用命令-文件处理命令-链接命令
- [Python开发] python使用socket通信
- java switch 用法
- 第28章 锁
- MinGW编译安装VTK-5.10.1_支持Qt4.8.5
- Spring 注解总结