leetcode 44. Wildcard Matching

来源:互联网 发布:ubuntu 运行c 编辑:程序博客网 时间:2024/06/03 10:16

leetcode 44. Wildcard Matching

这是一段拙劣的程序: Acc at the end in complexity ranking

public class Solution {public static void main(String[] arg){String a = "aa";String b = "*";Solution s = new Solution();boolean re = s.isMatch(a,b);}    public int max(int a,int b){        return a>b?a:b;    }        public boolean isMatch(String s, String p) {        int sl = s.length();        int pl = p.length();                if(sl==0){            for(int h=0;h<pl;h++){                if(p.charAt(h)!='*')  return false;            }            return true;        }        if(pl==0)  return false;        int[][] dp = new int[sl+1][pl+1];        //pre                for(int i=0;i<=sl;i++){            for(int j=0;j<=pl;j++){                dp[i][j] = -1;            }        }        dp[0][0] = 1;        for(int i=1;i<=sl;i++){            for(int j=1;j<=pl;j++){                                if( p.charAt(j-1)=='*' ){                    for(int k=i-1;k<=sl;k++){                        dp[k][j] = max(dp[i-1][j-1],dp[k][j]);                    }                    for(int k=i;k<=sl;k++){                        dp[k][j] = max(dp[i][j-1],dp[k][j]);                    }                }                if( s.charAt(i-1)==p.charAt(j-1) || p.charAt(j-1)== '?' ){                    dp[i][j] = max(dp[i-1][j-1],dp[i][j]);                }            }        }        if(dp[sl][pl]==1)  return true;           return false;    }}


0 0
原创粉丝点击