美文网首页动态规划
📚 44. Wildcard Matching

📚 44. Wildcard Matching

作者: 沉睡至夏 | 来源:发表于2016-12-24 05:41 被阅读31次

    LeetCode里面两个expression matching问题都是各家面试的重点

    1. 和 44.
      找出recursive是关键啊。
    public class Solution {
        public boolean isMatch(String s, String p) {
            int m = s.length();
            int n = p.length();
            
            boolean dp[][] = new boolean[m+1][n+1];
            dp[0][0] = true;
            // s is empty:
            for(int j=1; j<=n; j++) {
                if (p.charAt(j-1) != '*')
                    break;
                else 
                    dp[0][j] = true;
            }
            // fill the table:
            for(int i=1; i<=m; i++) {
                for(int j=1; j<=n; j++) {
                    char c = p.charAt(j-1);
                    if(c != '*')
                        dp[i][j] = dp[i-1][j-1] && (s.charAt(i-1) == c || c == '?');
                    else 
                        dp[i][j] = dp[i-1][j] || dp[i][j-1];
                }
            }
            return dp[m][n];
        }
    }
    

    嫌弃自己效率不高啊。特别是刷到晚上,脑袋就不清醒了。
    效率,效率,效率。重要的事情讲三遍!

    相关文章

      网友评论

        本文标题:📚 44. Wildcard Matching

        本文链接:https://www.haomeiwen.com/subject/wajlvttx.html