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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

leetcode-44. Wildcard Matching

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

題目闡釋:

正則匹配字符串,用程序實現

關鍵理解:

正則匹配,動態規劃思想,一個個向后追溯,后面的依賴前面的匹配成功。 正則和待匹配的字符串長度不一,統一到正則字符串的index索引上,每次的字符串index移動,都以匹配到的正則的index為準。 正則由于*?的存在,所以有多種狀態,中間狀態儲存都需要記錄下來。然后以這些狀態為動態的中轉,繼續判斷到最后。 最后正則匹配字符串是否成功的判斷依據,就是正則字符串的最大index,是否出現在遍歷到最后的狀態列表中。

錯誤之處:

多處動態變化,導致無法入手,*沒有處理思路,沒有找到匹配成功的條件

應用:

正則屬于多條路徑問題,可以推理到 多種渠道的問題,匹配成功當前的才往后推 *相當于無限向后匹配,所以無限循環使用,看能否匹配成功。
  • 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)

    總結

    以上是生活随笔為你收集整理的leetcode-44. Wildcard Matching的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。