leetcode392 判断子序列

1.双指针法

class Solution {
public:
    bool isSubsequence(string s, string t) {
        int i=0,j=0; //两个指针,i为s的指针,j为t的指针
        while(i<s.size()&&j<t.size())
        {
            while(t[j]!=s[i] && j<t.size()) ++j;  //在t中找s里i对应的字母
            if(j<t.size()) {++i;++j;}
            
        }
        if(i>=s.size())
            return true;
        else
            return false;        
    }
};

2.先处理长序列,合适有多个子串要查找

class Solution {
public:
    //abcada  序列t  cd序列s
    bool isSubsequence(string s, string t) {
        vector<vector<int>> record(26); //预分配空间,可以避免vector的空间扩展浪费时间
        for(int i=0;i<t.size();++i)
            record[t[i]-'a'].push_back(i);  //二维列表,下标为字母,一维vector为字母对应的下标列表,即该字母在t中所有出现的位置,a出现的位置为0,3,5
        int last_index=-1; //记录s中上一个字母在t中出现的位置
        for(int i=0;i<s.size();++i)
        {
            auto iter=upper_bound(record[s[i]-'a'].begin(),record[s[i]-'a'].end(),last_index); //二分法查找s中当前字母在t中的位置,要求序列是有序的,切从小到大排列,该位置应该大于上一个字母的位置last_index,因此用upper_bound
            if(iter==record[s[i]-'a'].end()) return false; //满足条件的字母在t中没找到
            last_index=*iter; //当前字母的位置为last_index,比如s中当前字母在t中的位置为5,则下个字母在t中的位置从6开始找
        }
        return true;        
    }
};

参考
https://leetcode.com/problems/is-subsequence/discuss/87408/Binary-search-solution-to-cope-with-input-with-many-Ss(with-explanation)

https://blog.csdn.net/camellhf/article/details/52713884

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值