Leetcode(python)--10.Regular Expression Matching
来源:互联网 发布:广东网络电视营业厅 编辑:程序博客网 时间:2024/06/05 01:10
如:
isMatch(“aa”,”a”) → false
isMatch(“aa”,”aa”) → true
isMatch(“aaa”,”aa”) → false
isMatch(“aa”, “a*”) → true
isMatch(“aa”, “.*”) → true
isMatch(“ab”, “.*”) → true
isMatch(“aab”, “c*a*b”) → true
不同于我们平时用的正则表达式,这道题限制成了简易正则表达式,只有两个规则。
python代码解决如下(ps:借鉴了网上的答案)
class Solution(object): def isMatch(self,s, p): # s能被p匹配 """ :type s: str :type p: str :rtype: bool """ lens = len(s) lenp = len(p) res = [] for i in range(lens + 1): t = [] for j in range(lenp + 1): t.append(False) res.append(t) res[lens][lenp] = True # res = [[False for i in range(lenp + 1)] for i in range(lens + 1)] for i in range(lenp - 1, -1, -1): # lenp-1到-1(不包含-1即到0) if p[i] == "*": # 为什么逆着,因为*号总在右边 res[lens][i] = res[lens][i + 1] elif i + 1 < lenp and p[i + 1] == "*": res[lens][i] = res[lens][i + 1] else: res[lens][i] = False for i in range(lens - 1, -1, -1): # s的所有下标从大到小 for j in range(lenp - 1, -1, -1): # p的所有下标 if p[j] == "*": if j - 1 >= 0 and p[j - 1] != "*": # 注意两个*不能匹配 res[i][j] = res[i][j + 1] else: return False elif j + 1 < lenp and p[j + 1] == "*": if s[i] == p[j] or p[j] == ".": res[i][j] = res[i][j + 2] or res[i + 1][j] or res[i + 1][j + 2] else: res[i][j] = res[i][j + 2] else: if s[i] == p[j] or p[j] == ".": res[i][j] = res[i + 1][j + 1] else: res[i][j] = False return res[0][0]
0 0
- Leetcode(python)--10.Regular Expression Matching
- 10. Regular Expression Matching Leetcode Python
- [Leetcode]10. Regular Expression Matching @python
- LeetCode 10. Regular Expression Matching(hard)
- leetcode 10. Regular Expression Matching(dp)
- [LeetCode]10.Regular Expression Matching
- LeetCode --- 10. Regular Expression Matching
- [Leetcode] 10. Regular Expression Matching
- [LeetCode]10.Regular Expression Matching
- [leetcode] 10.Regular Expression Matching
- Leetcode-10.Regular Expression Matching
- leetcode 10. Regular Expression Matching
- leetcode 10. Regular Expression Matching
- Leetcode 10. Regular Expression Matching
- leetcode 10. Regular Expression Matching
- Leetcode 10. Regular Expression Matching
- leetcode 10. Regular Expression Matching
- LeetCode-10. Regular Expression Matching
- hdu 题目分类
- POJ 1579-Function Run Fun(记忆化搜索-递归)
- Matlab下调用USB串口发送trigger信号
- Tomcat的搭建方式之EXE文件搭建
- 编程题:间隔相同
- Leetcode(python)--10.Regular Expression Matching
- /sysfs探索_store和show的接口
- 常用的不同时间类型的相互转换
- DOBRI
- How to implement xpcom module in JavaScript
- mysql性能探究
- [数据结构]克鲁斯卡尔(Kruskal)算法
- 被Arr Object遗忘的孩子:Arr.filter()
- 成为一个高级java架构师所需要具备那些技能呢?