leetCode第44题. Wildcard Matching(java实现)

题目描述:

leetCode第44题. Wildcard Matching(java实现)_第1张图片

题目解答(动态规划法解题):

class Solution {
    public boolean isMatch(String s, String p) {
        //动态规划法
        //构建变量进行判断,变量长度为s.length+1,注意:要加1
        boolean[][] match=new boolean[s.length()+1][p.length()+1];
        //对最后一个数值进行初始化
        match[s.length()][p.length()]=true;
        for(int i=s.length()-1;i>=0;i--){
            for(int j=p.length()-1;j>=0;j--){
                //有下面一下三种判断情况
                //第一种情况,如果数值相同或者通配符?匹配,则此处的match与对应+1的相同
                if(s.charAt(i)==p.charAt(j)||p.charAt(j)=='?')
                    match[i][j]=match[i+1][j+1];
                else if(p.charAt(j)=='*')//第二种情况是与通配符*匹配
                    match[i][j]=match[i+1][j]||match[i][j+1];
                else
                    match[i][j]=false;
            }
        }
        return match[0][0];
        
    }
}

运行结果:

leetCode第44题. Wildcard Matching(java实现)_第2张图片

 

你可能感兴趣的:(LeetCode习题集,LeetCode习题集)