日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

leetcode-44. Wildcard Matching

發(fā)布時(shí)間:2024/4/13 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 leetcode-44. Wildcard Matching 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目闡釋:

正則匹配字符串,用程序?qū)崿F(xiàn)

關(guān)鍵理解:

正則匹配,動(dòng)態(tài)規(guī)劃思想,一個(gè)個(gè)向后追溯,后面的依賴前面的匹配成功。 正則和待匹配的字符串長(zhǎng)度不一,統(tǒng)一到正則字符串的index索引上,每次的字符串index移動(dòng),都以匹配到的正則的index為準(zhǔn)。 正則由于*?的存在,所以有多種狀態(tài),中間狀態(tài)儲(chǔ)存都需要記錄下來(lái)。然后以這些狀態(tài)為動(dòng)態(tài)的中轉(zhuǎn),繼續(xù)判斷到最后。 最后正則匹配字符串是否成功的判斷依據(jù),就是正則字符串的最大index,是否出現(xiàn)在遍歷到最后的狀態(tài)列表中。

錯(cuò)誤之處:

多處動(dòng)態(tài)變化,導(dǎo)致無(wú)法入手,*沒有處理思路,沒有找到匹配成功的條件

應(yīng)用:

正則屬于多條路徑問(wèn)題,可以推理到 多種渠道的問(wèn)題,匹配成功當(dāng)前的才往后推 *相當(dāng)于無(wú)限向后匹配,所以無(wú)限循環(huán)使用,看能否匹配成功。
  • Wildcard Matching
  • Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'.

    '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence).The matching should cover the entire input string (not partial). Note: s could be empty and contains only lowercase letters a-z. p could be empty and contains only lowercase letters a-z, and characters like ? or *. Example 1:

    Input:

    s = "aa" p = "a" Output: false
    Explanation: "a" does not match the entire string "aa".

    Example 2:

    Input:

    s = "aa" p = "*" Output: true
    Explanation: '*' matches any sequence.

    Example 3:

    Input:

    s = "cb" p = "?a" Output: false
    Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'.

    Example 4:

    Input:

    s = "adceb" p = "*a*b" Output: true
    Explanation: The first '' matches the empty sequence, while the second '' matches the substring "dce".

    Example 5:

    Input:

    s = "acdcb" p = "a*c?b" Output: false class Solution(object):def isMatch(self, s, p):""":type s: str:type p: str:rtype: bool"""transfer = {}index=0for char in p:if char=='*':transfer[index,char]=indexelse:transfer[index,char]=index+1index+=1accept=index# index=0state = {0}for char in s:state_tmp=set()for index in state:for char_prob in [char,'?','*']:index_next=transfer.get((index,char_prob))state_tmp.add(index_next)state=state_tmpreturn accept in stateif __name__=='__main__':s = "acdcb"p = "a*c?b"p = "a**c?d"st=Solution()out=st.isMatch(s,p)print(out)

    總結(jié)

    以上是生活随笔為你收集整理的leetcode-44. Wildcard Matching的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。