【题解】392. 判断子序列 (字符串、双指针、队列)

https://leetcode.cn/problems/is-subsequence/description/?envType=study-plan-v2&envId=top-interview-150
在这里插入图片描述

  1. 双指针
class Solution {
public:
    bool isSubsequence(string s, string t) {
        int m = s.size(), n = t.size();
        int i = 0, j = 0;
        while (i < m && j < n) {
            if (s[i] == t[j]) {
                ++i;
            }
            ++j;
        }

        return i == m;
    }
};
  1. 队列
class Solution {
public:
    bool isSubsequence(string s, string t) {
        queue<int> q;
        for (auto e : s)
        {
            q.push(e);
        }
        for (auto e : t)
        {
            if (e == q.front())
            {
                q.pop();
            }
        }
        return q.empty();
    }
};

相关推荐

  1. 【面试经典150题】【指针392. 判断序列

    2024-07-21 21:18:03       26 阅读
  2. Leetcode 392 判断序列

    2024-07-21 21:18:03       33 阅读
  3. leetcode392--判断序列

    2024-07-21 21:18:03       30 阅读
  4. Leetcode 392. 判断序列

    2024-07-21 21:18:03       17 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-21 21:18:03       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-21 21:18:03       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-21 21:18:03       45 阅读
  4. Python语言-面向对象

    2024-07-21 21:18:03       55 阅读

热门阅读

  1. 【深度学习】sdxl的Lora训练技巧

    2024-07-21 21:18:03       18 阅读
  2. 理解Cookie、Session和Token

    2024-07-21 21:18:03       15 阅读
  3. 第四节shell条件测试(5)

    2024-07-21 21:18:03       17 阅读
  4. Python内存泄漏排查

    2024-07-21 21:18:03       16 阅读
  5. 【瓴岳科技】历史面试题

    2024-07-21 21:18:03       18 阅读
  6. 揭秘Odoo OWL的魔法:reactive vs useState

    2024-07-21 21:18:03       15 阅读
  7. PS像素图层简介

    2024-07-21 21:18:03       16 阅读