LeetCode正则表达式-Regular Expression Matching
发布时间:2020-12-14 01:59:55 所属栏目:百科 来源:网络整理
导读: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
Implement regular expression matching with support for '.' 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") → 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
public boolean isMatch(String s,String p) { if(p.length()==0||p==null){ return s==null||s.length()==0; } if(s==null){ return false; } if(s.length()==0){ if(p.length()%2==1) return false; else{ for(int i=0;i<p.length();i++){ if(i%2==1&&p.charAt(i)!='*'){ return false; } } return true; } } if(p.length() == 1){ if(s.length()>1){ return false; } if(p.charAt(0) == s.charAt(0)||p.charAt(0)=='.'){ return true; } return false; } else{ if(p.charAt(1)=='*'){ 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)); } else{ if(p.charAt(0)==s.charAt(0)||p.charAt(0)=='.'){ return isMatch(s.substring(1),p.substring(1)); } return false; } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- bootloader程式邏輯(8250 board)
- ruby-on-rails – Rails production.log文件为空
- 如何在C#中创建一个侵入式树类使用泛型?
- objective-c – 如何将boolean解析为NSString
- Using Flashback Drop and Managing the Recycle Bin
- sqlite3_part1
- Oracle 等待事件之 db file scattered read
- Ajax、Json对象及完成添加购物车的功能
- 无法在Ruby on Rails 3中将csv导入MySQL数据库
- Reactjs开发自制编程语言Monkey的编译器:语法解析