LeetCode: 44. Wildcard Matching
LeetCode: 44. Wildcard Matching
题目描述
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).
The function prototype should be:
bool isMatch(const char *s, const char *p)Some examples:
isMatch(“aa”,”a”) → false
isMatch(“aa”,”aa”) → true
isMatch(“aaa”,”aa”) → false
isMatch(“aa”, “*”) → true
isMatch(“aa”, “a*”) → true
isMatch(“ab”, “?*”) → true
isMatch(“aab”, “c*a*b”) → false
题目大意: 给定模式串 p, 匹配串 s。其中,p含有通配符 (Wildcard)。 ‘?’ 代表可以匹配任意一个字符, ‘*’ 代表可以匹配任意个数任意字符。 判断 p 串 和 s 串是否匹配。
解题思路 — 动态规划
- 记s串的前i个字符和p串的前j个字符能否匹配为
dp[i][j]
。 - 如果模式串p和匹配串s都为空,则匹配成功,
dp[0][0] = true;
。 - 如果模式串p为空,s串不为空, 则匹配失败,
dp[i][0] = false
, 其中0 < i <= s.size()
。 如果模式串p不为空, s串为空,i > 0, 若
p[i-1] == '*'
则dp[0][i]
的真值受dp[0][i-1] 的影响。
否则,dp[0][i] = false
。即,当p串和s串都不为空时, 状态转移方程如下,
AC代码
class Solution {
public:bool isMatch(string s, string p) {vector<vector<bool>> dp;dp.resize(s.size()+1);for(auto& vb : dp) vb.resize(p.size()+1, false);dp[0][0] = true;for(int i = 1; i <= p.size(); ++i){if(p[i-1] == '*') dp[0][i] = dp[0][i-1];}for(int i = 1; i <= s.size(); ++i){for(int j = 1; j <= p.size(); ++j){if(s[i-1] == p[j-1] || p[j-1] == '?'){dp[i][j] = dp[i-1][j-1];}else if(p[j-1] == '*'){for(int k = 0; k <= i; ++k){if(dp[k][j-1] == true){dp[i][j] = true;break;}}}else{dp[i][j] = false;}}}return dp[s.size()][p.size()];}
};
LeetCode: 44. Wildcard Matching相关推荐
- 【To Understand! 重点 递归 动态规划 正则表达式匹配】LeetCode 44. Wildcard Matching
LeetCode 44. Wildcard Matching Solution1:我的答案 递归,时间复杂度是O(2n)O(2n)O(2^n)因为超时未能AC 只是记录一下,以警示后人-- class ...
- LeetCode - 44. Wildcard Matching
44. Wildcard Matching Problem's Link --------------------------------------------------------------- ...
- [leetcode 44] Wildcard Matching
Implement wildcard pattern matching with support for '?' and '*'. '? ' Matches any single character. ...
- [LeetCode] #44 Wildcard Matching
mplement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ' ...
- LeetCode 10. Regular Expression Matching / 44. Wildcard Matching
10. Regular Expression Matching 经典DP题目,比较复杂,需要多复习. dp[i][j] 表示 s 下标0~i,p 下标0~j 是否能够匹配 dp[i-1][j-1] ...
- 【LeetCode】44. Wildcard Matching (2 solutions)
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
- 44. Wildcard Matching 通配符匹配
Title 给定一个字符串 (s) 和一个字符模式 § ,实现一个支持 '?' 和 '*' 的通配符匹配. '?' 可以匹配任何单个字符. '*' 可以匹配任意字符串(包括空字符串). 两个字符串完全 ...
- [OJ] Wildcard Matching (Hard)
LintCode 192. Wildcard Matching (Hard) LeetCode 44. Wildcard Matching (Hard) 第二次刷还是被这题虐. 其实就是跪在一个地方, ...
- [LeetCode] Wildcard Matching 题解
6. Wildcard Matching 题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matche ...
最新文章
- Visual Studio 2008 每日提示(十四)
- POJ 1679 - The Unique MST(次小生成树)
- wxWidgets:wxAuiToolBar类用法
- 《IPv6安全》——1.7 推荐读物和资料
- 模拟——玩具谜题(洛谷 P1563)
- 内存泄露之常见问题解决「初级篇」
- 文字处理技术:终于明白了压缩要怎么做
- JAVA爬虫挖取CSDN博客文章(续)
- 堪萨斯州立大学计算机科学,堪萨斯州立大学有哪些专业?
- 机器学习入门-西瓜书总结笔记
- 智慧灯杆新功能:微雾降尘
- 实用的自然码双拼口诀
- P9:最大池化的使用
- 刺猬乐队在唯品会工作过_Microsoft乐队是您从未听说过的出色智能手表和健身追踪器...
- 交换机和BBU的接口编号以及华为ATN950 BBU接口写法
- BootStrap的使用教程,适合新手小白!!
- 使用element回到顶部组件报错“Error: target is not existed: .page-component__scroll .el-scrollbar__wrap“
- java用户名重复验证代码_java如何让注册的用户名不重复,在当前页面就可以判断,并抛出提示?...
- 二进制,八进制,十进制,十六进制转换算法
- 在查询分析器里执行数据库的备份和还原操作
热门文章
- “中国式支付”在全球成功逆袭
- Ardupilot 串口代码学习
- 605元买了Java教程自学半年后…… How2J怎么样?我的Java自学心得
- 如何在android设备上insmod自己单独编译的.ko,解决签名问题
- mysql权限系统的工作原理_Mysql权限系统工作原理_PHP教程
- OGNL表达式的使用方法
- Cows(树状数组)
- Xpdf 中文字体解决方案(TTF字库) - 图文教程
- MIPI D-PHYv2.5笔记(1) -- DPHY概览
- nginx 如何配置来获取用户真实IP