码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]Wildcard Matching

时间:2016-01-10 16:55:15      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

public class Solution {
    public boolean isMatch(String s, String p) {
        int length1 = s.length();
        int length2 = p.length();
        if (length1 == 0) {
            return p.replace("*", "").length() == 0;
        }
        boolean[] record = new boolean[length1 + 1];
        record[0] = true;
        boolean allstar = true;
        for (int i = 0; i < length2; i++) {
            if (allstar)
                allstar = p.charAt(i) == ‘*‘;
            for (int j = length1 - 1; j >= 0; j--) {
                if (p.charAt(i) != ‘*‘) {
                    if (p.charAt(i) == s.charAt(j) || p.charAt(i) == ‘?‘) {
                        record[j + 1] = record[j];
                    } else {
                        record[j + 1] = false;
                    }
                } else {
                    boolean flg = false;
                    for (int jj = j + 1; jj >= 0; jj --) {
                        if (record[jj]) {
                            flg = true;
                            break;
                        }
                    }
                    record[j + 1] = flg;
                }
            }
            record[0] = allstar;
        }
        return record[length1];
    }
}

 

[LeetCode]Wildcard Matching

标签:

原文地址:http://www.cnblogs.com/vision-love-programming/p/5118585.html

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