首页 > 其他 > 详细

LeetCode 44 Wildcard Matching(字符串匹配问题)

时间:2017-03-13 13:32:39      阅读:268      评论:0      收藏:0      [点我收藏+]

 
‘?‘ Matches any single character.
‘*‘ Matches any sequence of characters (including the empty sequence).

字符串匹配问题。
如上所示:其中 ‘ ?’ 可以匹配任何一个单字符    ’ * ‘ 可以匹配任意长度的字符包括空字符串
 给定字符串s,和字符串p。判断按照以上规则,字符串p是否能够匹配字符串s
 
 
参考代码: 
package leetcode_50;


/***
 * 
 * @author pengfei_zheng
 * 字符串匹配问题
 */
public class Solution44 {
    public boolean isMatch(String s, String p) {
        int sp = 0, pp = 0, match = 0, starIdx = -1;            
        while (sp < s.length()){
            if (pp < p.length()  && (p.charAt(pp) == ‘?‘ 
                    || s.charAt(sp) == p.charAt(pp))){
                sp++;
                pp++;
            }
            else if (pp < p.length() && p.charAt(pp) == ‘*‘){
                starIdx = pp;
                match = sp;
                pp++;
            }
            else if (starIdx != -1){
                pp = starIdx + 1;
                match++;
                sp = match;
            }
            else return false;
        }
        while (pp < p.length() && p.charAt(pp) == ‘*‘)
            pp++;
        
        return pp == p.length();
    }
}

 

 
 

LeetCode 44 Wildcard Matching(字符串匹配问题)

原文:http://www.cnblogs.com/zpfbuaa/p/6542144.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!