- leet_code:链接
- 问题描述:判断序列a是否为序列b的子序列
- 输入输出样例:
- intput :a = “abc”, b = “ahbgdc”
- output: true(1).
- c++ 代码实现
class Solution
{
public:
bool isSubsequence(string S1, string S2)
{
int idx = 0, end = S1.size();
for (char val : S2)
{
if (S1[idx] == val)
idx += 1;
if (idx == end)
return true;
}
return false;
}
};
主调函数:
#include<iostream>
using namespace std;
#include<string>
int main(int argc, char* argv[])
{
string S1 = "abc", S2 = "ahbgdc";
Solution solution;
cout << solution.isSubsequence(S1, S2) << endl;
system("pause");
return 0;
}
本文介绍了一个LeetCode上的经典题目,即判断一个字符串是否为另一个字符串的子序列。通过一个C++代码实例,详细展示了如何使用双指针技巧解决这一问题。主调函数演示了如何使用该解决方案进行实际测试。

2703

被折叠的 条评论
为什么被折叠?



