[LeetCode]44. Wildcard Matching
来源:互联网 发布:房产中介软件hao123 编辑:程序博客网 时间:2024/05/29 09:43
https://leetcode.com/problems/wildcard-matching/
这里面要处理的就是*,如果遇到*,那么用matchIdx记录已经匹配的str位置,starIdx表示*所在位置。当遇到*时,最开始让*表示0个str里面的字符,所以p++;之后再匹配,如果s和p有匹配不上,那么让*表示一个字符,之后依次类推。
public class Solution { public boolean isMatch(String str, String pattern) { int s = 0; int p = 0; int starIdx = -1; int matchIdx = 0; while (s < str.length()) { if (p < pattern.length() && (str.charAt(s) == pattern.charAt(p) || pattern.charAt(p) == '?')) { s++; p++; } else if (p < pattern.length() && pattern.charAt(p) == '*') { matchIdx = s; starIdx = p; p++; } else if (starIdx != -1) { // 使用match++ & sIndex = match而不是直接sIndex = match + 1是因为如果后续所有s和p均不匹配,sIndex可以一直自增,而不是死循环 s = ++matchIdx; p = starIdx + 1; } else { return false; } } while (p < pattern.length() && pattern.charAt(p) == '*') { p++; } return p == pattern.length(); }}
0 0
- LeetCode --- 44. Wildcard Matching
- [Leetcode] 44. Wildcard Matching
- Leetcode 44. Wildcard Matching
- leetcode 44. Wildcard Matching
- Leetcode 44. Wildcard Matching
- Leetcode:44. Wildcard Matching
- LeetCode 44. Wildcard Matching
- leetcode.44. Wildcard Matching
- LeetCode 44. Wildcard Matching
- Leetcode-44. Wildcard Matching
- [LeetCode] 44. Wildcard Matching
- [leetcode] 44. Wildcard Matching
- [LeetCode]--44. Wildcard Matching
- LeetCode 44. Wildcard Matching
- leetcode 44. Wildcard Matching
- LeetCode 44. Wildcard Matching
- 44. Wildcard Matching -- LeetCode
- LeetCode 44. Wildcard Matching
- 如何转载CSDN博客
- 采用队列数据结构按层次遍历二叉树
- android 如何设置颜色的透明度!
- Floyd-Warshall算法求任意两点间的最短路(图论算法)
- 视频列表的android客户端和springmvc服务端实现(三)
- [LeetCode]44. Wildcard Matching
- 数值分析 第五章 插值与逼近
- Winform打开和关闭控制台
- Fragment中监听返回键
- 算法分析:归并排序
- JavaScript 火焰
- 深度堆栈网络训练Python代码
- GOGS自动启动
- SSM项目搭建时遇到的问题