zoukankan      html  css  js  c++  java
  • leetcode每日一题(2020-07-05):44. 通配符匹配

    题目描述:
    给定一个字符串 (s) 和一个字符模式 (p) ,实现一个支持 '?' 和 '*' 的通配符匹配。

    '?' 可以匹配任何单个字符。
    '*' 可以匹配任意字符串(包括空字符串)。
    

    两个字符串完全匹配才算匹配成功。

    今日学习:
    1.动规,这次就差一点就做出来了

    题解:
    1.我考虑问题的时候有一块想简单了【初始化dp[0][j]】,有一块想复杂了【?和相等的时候直接等于左上角就可以,不需要知道前面是什么】
    2.看注释吧

    var isMatch = function(s, p) {
        let dp = new Array(s.length + 1)
        for(let i = 0; i < dp.length; i++) {
            dp[i] = new Array(p.length + 1).fill(false)
        }
        //p和s都为空时可以匹配
        dp[0][0] = true
        //若p中只有*,则可以任意匹配s
        if(p[0] == '*') {
            for(let i = 0; i <= s.length; i++) {
                dp[i][1] = true
            }
        }
    //这里没初始化dp[0][j]
        for(let i = 0; i < s.length; i++) {
            for(let j = 0; j < p.length; j++) {
    //这里想复杂了
                if(p[j] == '?' || p[j] == s[i]) {
                    if(p[j - 1] == '*') {
                        dp[i + 1][j + 1] = dp[i + 1][j]
                    }else {
                        dp[i + 1][j + 1] = dp[i][j]
                    }
                }
                if(p[j] == "*") {
    //这里少考虑了一种情况
                    if(dp[i][j + 1] == true) {
                        dp[i + 1][j + 1] = true
                    }else {
                        dp[i + 1][j + 1] = dp[i + 1][j]
                    }
                }
            }
        }
        return dp[s.length][p.length]
    };
    var isMatch = function(s, p) {
        let sLen = s.length, pLen = p.length
        let dp = new Array(sLen + 1)
        for(let i = 0; i <= sLen; i++) {
            dp[i] = new Array(pLen + 1).fill(false)
        }
        // p和s都为空时可以匹配成功
        dp[0][0] = true
        // 若p中只有*,则可以任意匹配的s
        if(p[0] == '*') {
            for(let i = 0; i <= sLen; i++) {
                dp[i][1] = true
            }
        }
        // 初始化dp第0行,即s为空字符串的情况
        // 只有连续的'*'才能匹配空字符串
        for(let j = 1; j <= pLen; j++) {
            dp[0][j] = p[j - 1] == '*' && dp[0][j - 1]
        }
        for(let i = 0; i < sLen; i++) {
            for(let j = 0; j < pLen; j++) {
                // '?'和相同的字符串地位相等
                if(p[j] == '?' || p[j] == s[i]) {
                    dp[i + 1][j + 1] = dp[i][j]
                }
                // *可以匹配任意新字符,也可以当作空字符使用
                if(p[j] == "*") {
                    if(dp[i][j + 1] == true || dp[i + 1][j] == true) {
                        dp[i + 1][j + 1] = true
                    }
                }
            }
        }
        return dp[sLen][pLen]
    };
    
  • 相关阅读:
    MySQL 一致性读 深入研究
    Operating System Error Codes
    5分钟了解MySQL5.7的Online DDL雷区
    pt-osc原理、限制、及与原生online-ddl比较
    学习笔记:Rick's RoTs -- Rules of Thumb for MySQL
    学习笔记:The Best of MySQL Forum
    学习笔记:Analyze MySQL Performance及慢日志的开启
    MySQL: Building the best INDEX for a given SELECT
    学习笔记:ALTERing a Huge MySQL Table
    Google common_schema 2.2 documentation
  • 原文地址:https://www.cnblogs.com/autumn-starrysky/p/13245737.html
Copyright © 2011-2022 走看看