C++ | Leetcode C++题解之第44题通配符匹配

题目:

题解:

cpp 复制代码
class Solution {
public:
    bool isMatch(string s, string p) {
        auto allStars = [](const string& str, int left, int right) {
            for (int i = left; i < right; ++i) {
                if (str[i] != '*') {
                    return false;
                }
            }
            return true;
        };
        auto charMatch = [](char u, char v) {
            return u == v || v == '?';
        };

        while (s.size() && p.size() && p.back() != '*') {
            if (charMatch(s.back(), p.back())) {
                s.pop_back();
                p.pop_back();
            }
            else {
                return false;
            }
        }
        if (p.empty()) {
            return s.empty();
        }

        int sIndex = 0, pIndex = 0;
        int sRecord = -1, pRecord = -1;
        while (sIndex < s.size() && pIndex < p.size()) {
            if (p[pIndex] == '*') {
                ++pIndex;
                sRecord = sIndex;
                pRecord = pIndex;
            }
            else if (charMatch(s[sIndex], p[pIndex])) {
                ++sIndex;
                ++pIndex;
            }
            else if (sRecord != -1 && sRecord + 1 < s.size()) {
                ++sRecord;
                sIndex = sRecord;
                pIndex = pRecord;
            }
            else {
                return false;
            }
        }
        return allStars(p, pIndex, p.size());
    }
};
相关推荐
恒者走天下30 分钟前
秋招落定,拿到满意的offer,怎么提高自己实际的开发能力,更好的融入团队
c++
做怪小疯子35 分钟前
LeetCode 热题 100——矩阵——旋转图像
算法·leetcode·矩阵
天若有情6731 小时前
【c++】手撸C++ Promise:从零实现通用异步回调组件,支持链式调用+异常安全
开发语言·前端·javascript·c++·promise
学困昇1 小时前
C++中的异常
android·java·c++
sin_hielo1 小时前
leetcode 2435
数据结构·算法·leetcode
合作小小程序员小小店2 小时前
桌面安全开发,桌面二进制%恶意行为拦截查杀%系统安全开发3.0,基于c/c++语言,mfc,win32,ring3,dll,hook,inject,无数据库
c语言·开发语言·c++·安全·系统安全
Codeking__2 小时前
C++ 11 atomic 原子性操作
开发语言·c++
crescent_悦2 小时前
PTA L1-020 帅到没朋友 C++
数据结构·c++·算法
卡提西亚2 小时前
C++笔记-34-map/multimap容器
开发语言·c++·笔记
2***B4493 小时前
C++在金融中的QuantLibXL
开发语言·c++·金融