Java for LeetCode 044 Wildcard Matching
Implement wildcard pattern matching with support for '?'
and '*'
.
- '?' Matches any single character.
- '*' Matches any sequence of characters (including the empty sequence).
- 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", "*") → true
- isMatch("aa", "a*") → true
- isMatch("ab", "?*") → true
- isMatch("aab", "c*a*b") → false
解题思路一:
参考之前写的Java for LeetCode 010 Regular Expression Matching,可以很轻松的用递归写出代码,JAVA实现如下:
- static public boolean isMatch(String s, String p) {
- if(s.length()==0){
- for(int i=0;i<p.length();i++)
- if(p.charAt(i)!='*')
- return false;
- return true;
- }
- if (p.length() == 0)
- return s.length() == 0;
- else if (p.length() == 1)
- return p.charAt(0)=='*'||(s.length() == 1&& (p.charAt(0) == '?' || s.charAt(0) == p.charAt(0)));
- if(p.charAt(0)!='*'){
- if(p.charAt(0)!=s.charAt(0)&&p.charAt(0)!='?')
- return false;
- return isMatch(s.substring(1),p.substring(1));
- }
- int index=0;
- while(index<p.length()){
- if(p.charAt(index)=='*')
- index++;
- else break;
- }
- if(index==p.length())
- return true;
- p=p.substring(index);
- for(int i=0;i<s.length();i++){
- if(isMatch(s.substring(i),p))
- return true;
- }
- return false;
- }
结果Time Limit Exceeded!也就是说这种类似暴力枚举的算法肯定不能通过!
解题思路二:
- static public boolean isMatch(String s, String p) {
- int indexS=0,indexP=0,starIndex=-2,sPosition=-2;
- while(indexS<s.length()){
- if(starIndex==p.length()-1)
- return true;
- if(indexP>=p.length()){
- if(starIndex<0)
- return false;
- indexP=starIndex+1;
- indexS=++sPosition;
- }
- if(s.charAt(indexS)==p.charAt(indexP)||p.charAt(indexP)=='?'){
- indexS++;
- indexP++;
- }
- else if(p.charAt(indexP)=='*'){
- starIndex=indexP++;
- sPosition=indexS;
- }
- else if(starIndex>=0){
- indexP=starIndex+1;
- indexS=++sPosition;
- }
- else return false;
- }
- while(indexP<p.length()){
- if(p.charAt(indexP)!='*')
- return false;
- indexP++;
- }
- return true;
- }
Java for LeetCode 044 Wildcard Matching的更多相关文章
- LeetCode 044 Wildcard Matching
题目要求:Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches ...
- 【leetcode】Wildcard Matching
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
- LeetCode - 44. Wildcard Matching
44. Wildcard Matching Problem's Link --------------------------------------------------------------- ...
- [LeetCode] 44. Wildcard Matching 外卡匹配
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...
- 【leetcode】Wildcard Matching(hard) ★ 大神太牛了
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- [leetcode]44. Wildcard Matching万能符匹配
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...
- LeetCode题解-----Wildcard Matching
题目描述: '?' Matches any single character. '*' Matches any sequence of characters (including the empty ...
- LeetCode 44 Wildcard Matching(字符串匹配问题)
题目链接:https://leetcode.com/problems/wildcard-matching/?tab=Description '?' Matches any single chara ...
- 044 Wildcard Matching 通配符匹配
实现一个支持 '?' 和 '*' 的通配符匹配.'?' 匹配任何单个字符.'*' 匹配任何数量的字符 (包括0个).匹配应覆盖 整个 输入字符串(而不是部分).这个函数原型为:bool isMatch ...
随机推荐
- Spring JdbcTemplate 的使用与学习(转)
紧接上一篇 (JdbcTemplate是线程安全的,因此可以配置一个简单的JdbcTemplate实例,将这个共享的实例注入到多个DAO类中.辅助的文档) Spring DAO支持 http://ww ...
- phpMyadmin /scripts/setup.php Remote Code Injection && Execution CVE-2009-1151
目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 Insufficient output sanitizing when gener ...
- Mybatis的ResultMap的使用
本篇文章通过一个实际工作中遇到的例子开始吧: 工程使用Spring+Mybatis+Mysql开发.具体的业务逻辑很重,对象之间一层一层的嵌套.和数据库表对应的是大量的model类,而和前端交互的是V ...
- IOS基础之 (七) 分类Category
一 Category 分类:Category(类目,类别) (OC有) 命名:原来的类+类别名(原来的类名自动生成,只要写后面的类别名,一般以模块名为名.比如原来类 Person,新建分类 Ct,新建 ...
- Aop 是面向切面编程,
Aop 是面向切面编程,是在业务代码中可以织入其他公共代码(性能监控等),现在用普通的方法实现AOP http://blog.csdn.net/heyanfeng22/article/details/ ...
- linux中软链接和硬链接的区别与小结
ln命令 该命令在文件之间创建链接.这种操作实际上是给系统中已有的某个文件指定另外一个可用于访问它的名称.对于这个新的文件名,我们可以为之指定不同的访问权限,以控制对信息的共享和安全性的问题. 如果链 ...
- 慎用 Enum.GetHashCode()
公司里遗留下了相当多的 Enum.GetHashCode()来获取枚举值的代码 但是这会产生装箱行为的!!因为Enum是值类型,GetHashCode()是Object的方法,调用GetHashCod ...
- 利用dedecms autoindex让文章列表加上序列号
有些时候我们在制作模板的需要在文章标题前面加上序列号,可以通过织梦自带的autoindex属性来实现,实现方法很简单,只需要在序号递增的地方加上 这段代码就行,[field:global runphp ...
- [LeetCode] Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- VIM、GVIM在WINDOWS下中文乱码的终极解决方案
文章转自:http://www.liuhuadong.com/archives/68 vim.gvim在windows下中文乱码的终极解决方案在windows下vim的中文字体显示并不好,所以我们需要 ...