Leetcode wild card matching
来源:互联网 发布:mac 如何装office 免费 编辑:程序博客网 时间:2024/05/16 18:14
public class Solution { private int[][] dp; public boolean isMatch(String s, String p) { if (s == null || p == null) { return false; } dp = new int[s.length()][p.length()]; return isMatchHelper(s, 0, p, 0); } private boolean isMatchHelper(String s, int indexS, String p, int indexP) { if (indexP >= p.length() || indexS >= s.length()) { return (indexP >= p.length() && indexS >= s.length()); } if (dp[indexS][indexP] == -1) { return false; } else if (dp[indexS][indexP] == 1) { return true; } char cp = p.charAt(indexP); boolean res = false; if (cp == '?') { // Match '?' res = isMatchHelper(s, indexS + 1, p, indexP + 1); } else if (cp == '*') { // Match '*' for (int i = indexS; i <= s.length(); i++) { boolean cur = isMatchHelper(s, i, p, indexP + 1); if (cur == true) { res = true; break; } } } else { // Match regular char if (cp == s.charAt(indexS)) { res = isMatchHelper(s, indexS + 1, p, indexP + 1); } } if (res == true) { dp[indexS][indexP] = 1; } else { dp[indexS][indexP] = -1; } return res; } }
在 recursion 的基础上加了 二维 DP 数组来储存之前的计算结果。
不过 leetcode 最后一个大集合还是过不去。
0 0
- Leetcode wild card matching
- wild card matching
- wild card 百搭牌;外卡
- Word Wild Card
- DDNS Wild Card
- [Google] URL query with wild card
- Regular Expression Matching in the Wild
- Regular Expression Matching in the Wild(中文)
- Codeforces VK Cup 2015 Wild Card Round 1 (AB)
- LeetCode: Regular Expression Matching
- LeetCode: Wildcard Matching
- LeetCode: Regular Expression Matching
- LeetCode : Wildcard Matching
- [LeetCode] Wildcard Matching - Iteratively
- leetcode 83: Wildcard Matching
- leetcode Regular Expression Matching
- [Leetcode] Wildcard Matching
- [Leetcode] Regular Expression Matching
- 水晶报表隐藏空值字段
- LeetCode(44)Permutations
- 手动添加mysql服务
- SD佛山ZD风
- oracle导入导出数据不完整问题
- Leetcode wild card matching
- python复习笔记
- 打架后
- 广义Fibonacci数列找循环节
- Get Minimal value in constant time from a LIFO(and FIFO) stack
- 【线性扫描+高精度】大整数除以小整数求余数,十进制除法
- Singleton
- python常用模块整理(更新中)
- Xcode快捷键