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 ...
随机推荐
- RPD资料库创建(1)
BI创建(数据)分析.仪表盘.报表前,都需要对数据进行建模,在oracle biee里称为创建“资料档案库”-该文件后缀为RPD,所以一般也称为创建RPD文件. 步骤: 1.从windows开始菜单里 ...
- Hibernate中一级缓存和二级缓存使用详解
一.一级缓存二级缓存的概念解释 (1)一级缓存就是Session级别的缓存,一个Session做了一个查询操作,它会把这个操作的结果放在一级缓存中,如果短时间内这个 session(一定要同一个ses ...
- 【CF刷题】14-05-12
Round 236 div.1 A:只需要每个点连接所有比他大的点,知道边用完为止. //By BLADEVIL #include <cmath> #include <cstdio& ...
- 【bzoj1016】 JSOI2008—最小生成树计数
http://www.lydsy.com/JudgeOnline/problem.php?id=1016 (题目链接) 题意 求图的最小生成树计数. Solution %了下题解,发现要写矩阵树,15 ...
- .net String.Format数字格式化输出
内容转载自:http://www.cnblogs.com/lqb/archive/2008/08/04/1259498.html 前面内容这个做的总结的很全,今后有新增的我继续往后补充.请留意我增加的 ...
- Syntax error, annotations are only available if source level is 1.5
在项目上右键 -> Properties -> Java Compiler
- 彻底理解position与anchorPoint
引言 相信初接触到CALayer的人都会遇到以下几个问题: 为什么修改anchorPoint会移动layer的位置?CALayer的position点是哪一点呢?anchorPoint与positio ...
- 解决Surface Pro外接移动硬盘经常睡眠的问题
1. 打开注册表,找到下面的键 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\PowerSettings\0012ee47-904 ...
- --hdu 2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(多重背包)
解题思路: 多重背包:第 i 件物品有 j 个可用. 本题中 第 p[i] 类大米 有 c[i] 袋大米可买 ,故本题为多重背包. n(总钱数).m(种类) p[i] 单价 h[i] 重量 c[i] ...
- unity3d下载Obb分包文件
下载OBB插件包 http://pan.baidu.com/s/1c0ouRZE 1.导入插件 注意事项: 如果项目中已经存在Android 插件,需要merge导入的xml文件例如 AndroidM ...