Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
coolalex776
Fucking Algorithm
提交
2cebc91e
F
Fucking Algorithm
项目概览
coolalex776
/
Fucking Algorithm
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
F
Fucking Algorithm
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
2cebc91e
编写于
3月 07, 2021
作者:
B
BruceCat
提交者:
GitHub
3月 07, 2021
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #527 from dekunma/392
【392. 判断子序列】【C++】
上级
730391bd
a72173b1
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
64 addition
and
2 deletion
+64
-2
高频面试系列/二分查找判定子序列.md
高频面试系列/二分查找判定子序列.md
+64
-2
未找到文件。
高频面试系列/二分查找判定子序列.md
浏览文件 @
2cebc91e
...
@@ -168,4 +168,67 @@ boolean isSubsequence(String s, String t) {
...
@@ -168,4 +168,67 @@ boolean isSubsequence(String s, String t) {
<img
src=
"../pictures/qrcode.jpg"
width=
200
>
<img
src=
"../pictures/qrcode.jpg"
width=
200
>
</p>
</p>
======其他语言代码======
======其他语言代码======
\ No newline at end of file
[
dekunma
](
https://www.linkedin.com/in/dekun-ma-036a9b198/
)
提供C++代码
**解法一:遍历(也可以用双指针):**
```
C++
class Solution {
public:
bool isSubsequence(string s, string t) {
// 遍历s
for(int i = 0; i < s.size(); i++) {
// 找到s[i]字符在t中的位置
size_t pos = t.find(s[i]);
// 如果s[i]字符不在t中,返回false
if(pos == std::string::npos) return false;
// 如果s[i]在t中,后面就只看pos以后的字串,防止重复查找
else t = t.substr(pos + 1);
}
return true;
}
};
```
**解法二:二分查找:**
```
C++
class Solution {
public:
bool isSubsequence(string s, string t) {
int m = s.size(), n = t.size();
// 对 t 进行预处理
vector<int> index[256];
for (int i = 0; i < n; i++) {
char c = t[i];
index[c].push_back(i);
}
// 串 t 上的指针
int j = 0;
// 借助 index 查找 s[i]
for (int i = 0; i < m; i++) {
char c = s[i];
// 整个 t 压根儿没有字符 c
if (index[c].empty()) return false;
int pos = left_bound(index[c], j);
// 二分搜索区间中没有找到字符 c
if (pos == index[c].size()) return false;
// 向前移动指针 j
j = index[c][pos] + 1;
}
return true;
}
// 查找左侧边界的二分查找
int left_bound(vector<int> arr, int tar) {
int lo = 0, hi = arr.size();
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (tar > arr[mid]) {
lo = mid + 1;
} else {
hi = mid;
}
}
return lo;
}
};
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录