zoukankan      html  css  js  c++  java
  • leetcode 010

    leetcode 010

    代码实现

    这道题用题目设定的语境去做的话,我暂时做不出来,只能看答案分析一下了。

    class Solution(object):
        def isMatch(self, text, pattern):
            if not pattern:
                return not text
    
            first_match = bool(text) and pattern[0] in {text[0], '.'}
    
            if len(pattern) >= 2 and pattern[1] == '*':
                return (self.isMatch(text, pattern[2:]) or
                        first_match and self.isMatch(text[1:], pattern))
            else:
                return first_match and self.isMatch(text[1:], pattern[1:])
    

    上面是答案里的递归做法,还有另一种动态规划做法。代码的大概思路就是,每次读取两组的首个字符,如果匹配满足,则将剩下的字符递归进入下一步。

    动态规划的思路我还没理清,暂时不贴上了。

  • 相关阅读:
    2016.7.31整机升级计划
    UVa 1588
    UVa1587
    Jzoj4714 公约数
    Jzoj4714 公约数
    Jzoj4713 A
    Jzoj4713 A
    Jzoj4711 Binary
    Jzoj4711 Binary
    Jzoj4710 Value
  • 原文地址:https://www.cnblogs.com/ChanWunsam/p/10246505.html
Copyright © 2011-2022 走看看