码迷,mamicode.com
首页 > 编程语言 > 详细

Wildcard Matching leetcode java

时间:2014-08-06 04:10:50      阅读:325      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   java   io   strong   for   

题目

 

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

题解
本文代码引用自:http://blog.csdn.net/perfect8886/article/details/22689147
 1     public boolean isMatch(String s, String p) {  
 2         int i = 0;  
 3         int j = 0;  
 4         int star = -1;  
 5         int mark = -1;  
 6         while (i < s.length()) {  
 7             if (j < p.length()  
 8                     && (p.charAt(j) == ‘?‘ || p.charAt(j) == s.charAt(i))) {  
 9                 ++i;  
10                 ++j;  
11             } else if (j < p.length() && p.charAt(j) == ‘*‘) {  
12                 star = j++;  
13                 mark = i;  
14             } else if (star != -1) {  
15                 j = star + 1;  
16                 i = ++mark;  
17             } else {  
18                 return false;  
19             }  
20         }  
21         while (j < p.length() && p.charAt(j) == ‘*‘) {  
22             ++j;  
23         }  
24         return j == p.length();  
25     }
 

Wildcard Matching leetcode java,布布扣,bubuko.com

Wildcard Matching leetcode java

标签:style   blog   http   color   java   io   strong   for   

原文地址:http://www.cnblogs.com/springfor/p/3893596.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!