【leetcode】 Wildcard Matching
来源:互联网 发布:音乐网站 知乎 编辑:程序博客网 时间:2024/06/10 01:05
from : https://leetcode.com/problems/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") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "*") → trueisMatch("aa", "a*") → trueisMatch("ab", "?*") → trueisMatch("aab", "c*a*b") → false
public class Solution { public boolean isMatch(String s, String p) { int i=0, j=0; int ls = s.length(), lp = p.length(); int statIdx = -1, sIdx = -1; while(i < ls) { if(j<lp && (s.charAt(i) == p.charAt(j) || '?' == p.charAt(j))) { //match ++i; ++j; continue; } if(j<lp && '*' == p.charAt(j)) { statIdx = j; // record star ++j; //match from next p sIdx = i; // record the position of s , star match 0 continue; } if(0 <= statIdx) { //if have star in front then backtrace j = statIdx + 1; //reset the position of p i = sIdx + 1; ++sIdx; //star match 1,2,3,4,5.... continue; } return false; //if not match return false } while(j < lp && '*' == p.charAt(j)) ++j; //skip continue star return j == lp; // successful match }}
class Solution {public: bool isMatch(string s, string p) { int i=0, j=0, ls = s.size(), lp = p.size(), is = -1, jp = -1; while(i < ls) { if(j < lp && (s[i] == p[j] || '?' == p[j])) { ++i; ++j; continue; } if(j < lp && '*' == p[j]) { is = i; jp = j; ++j; continue; } if(0 <= is) { ++is; i = is; j = jp + 1; continue; } return false; } while(j < lp && '*' == p[j]) ++j; return j == lp; }};
0 0
- LeetCode: Wildcard Matching
- LeetCode : Wildcard Matching
- [LeetCode] Wildcard Matching - Iteratively
- leetcode 83: Wildcard Matching
- [Leetcode] Wildcard Matching
- [LeetCode]Wildcard Matching
- LeetCode-Wildcard Matching
- LeetCode -- Wildcard Matching
- LeetCode | Wildcard Matching
- Leetcode: Wildcard Matching
- Leetcode:Wildcard Matching
- LeetCode题解:Wildcard Matching
- LeetCode Wildcard Matching
- [Leetcode] Wildcard Matching (Java)
- LeetCode OJ:Wildcard Matching
- Leetcode: Wildcard Matching
- Wildcard Matching -- LeetCode
- [LeetCode] Wildcard Matching
- Hibernate中的延迟加载机制
- Java JDBC 学习笔记 - 可更新的结果集
- myHttpRequestVolley
- 转战Androidstudio之项目eclipse迁移
- 快速排序
- 【leetcode】 Wildcard Matching
- CWinApp
- hdu 2563 -统计问题 【递推关系】
- KingbaseES数据库入门,注意事项
- cocos2d-x开发日志01——环境搭建和新建项目
- HTML的属性简介
- openvswitch中bridge的TAG使用
- 开灯问题
- 用hibernate和spring整合做小例子对数据库增删改查