zoukankan      html  css  js  c++  java
  • [leetcode]Wildcard Matching

    一看,直接递归...果然超时了TAT

    不过我觉得能把递归写对就不错啦,应该满足面试要求了吧.

    不过TLE我也不知道我写对没有.

    正确的做法是贪心.

    大概是这样的

    我们来匹配s和p

    如果匹配就s++ , p++

    如果不匹配的话就看p之前知否有*

    当然是否有*我们需要记录的,遇到*就记录当前*的位置和匹配到的s的位置

    然后从*的下一位置匹配,开始匹配0个字符

    如果ok往后走,往后不ok,那么匹配1个字符...同理2,3,4个字符(有点回溯的感觉吧

    所以实践复杂度是O(len(s) * len(p))

    class Solution {
    public:
        bool isMatch(const char *s, const char *p) {
            //? match one
            //* match 0,1,2,3..
            // aaaabc *c true
            const char* star = nullptr;
            const char* rs = nullptr;
            
            while(*s) {
                if(*s == *p || *p == '?') { //match
                    s++; p++;
                    continue;
                }
                if(*p == '*') { 
                    star = p; // record star
                    p++; //match from next p
                    rs = s; // record the position of s , star match 0
                    continue;
                } 
                if(star != nullptr) { //if have star in front then backtrace
                    p = star + 1; //reset the position of p 
                    s = rs + 1; 
                    rs ++; //star match 1,2,3,4,5....
                    continue;
                }
                return false; //if not match return false
            }
            while(*p == '*') p++; //skip continue star
            return *p == ''; // successful match
        }
    };
  • 相关阅读:
    opengl编程指南
    Binder机制1---Binder原理介绍
    [Android]使用platform密钥来给apk文件签名的命令
    IntentFilter
    最高分是多少
    Spring注入
    Bean容器的初始化
    Spring中的Junit
    IOC
    接口及面向接口编程
  • 原文地址:https://www.cnblogs.com/x1957/p/3517096.html
Copyright © 2011-2022 走看看