題目:
這道題主要應用遞歸的思路來進行匹配,值得注意的是规个,在python里 and 操作的優(yōu)先級是大于 or的凤薛,對遞歸不熟悉的朋友,不妨單步調(diào)試一下:
class Solution:
def isMatch(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
in_str = s
pt = p
if not pt:
return not in_str
first_match = bool(in_str) and pt[0] in {in_str[0], '.'}
if len(pt) >= 2 and pt[1] == '*':
return (self.isMatch(in_str, pt[2:])
or first_match and self.isMatch(in_str[1:], pt))
else:
return first_match and self.isMatch(in_str[1:], pt[1:])
s = Solution()
print(s.isMatch("ab", "c*ab"))
ps:如果您有好的建議诞仓,歡迎交流 :-D缤苫,也歡迎訪問我的個人博客:tundrazone.com