Skip to content

Commit 13ba9e9

Browse files
committed
[Feature] add for new
1 parent 71a6342 commit 13ba9e9

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
title: LC392. 判断子序列 is-subsequence
3+
date: 2025-08-31
4+
categories: [Leetcode-75]
5+
tags: [leetcode, Leetcode-75, two-pointer]
6+
published: true
7+
---
8+
9+
# LC392. 判断子序列 is-subsequence
10+
11+
给定字符串 s 和 t ,判断 s 是否为 t 的子序列。
12+
13+
字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。
14+
15+
(例如,"ace"是"abcde"的一个子序列,而"aec"不是)。
16+
17+
进阶:
18+
19+
如果有大量输入的 S,称作 S1, S2, ... , Sk 其中 k >= 10亿,你需要依次检查它们是否为 T 的子序列。在这种情况下,你会怎样改变代码?
20+
21+
示例 1:
22+
23+
输入:s = "abc", t = "ahbgdc"
24+
输出:true
25+
示例 2:
26+
27+
输入:s = "axc", t = "ahbgdc"
28+
输出:false
29+
30+
31+
提示:
32+
33+
0 <= s.length <= 100
34+
0 <= t.length <= 10^4
35+
两个字符串都只由小写字符组成。
36+
37+
# v1-双指针
38+
39+
## 思路
40+
41+
两个指针,同时从左往右移动。
42+
43+
ps 为 s 的指针;pt 为 t 的指针。
44+
45+
有相同的,则同时移动,否则就是一直移动到匹配的位置。
46+
47+
48+
49+
## 实现
50+
51+
```java
52+
public boolean isSubsequence(String s, String t) {
53+
int j = 0;
54+
int nt = t.length();
55+
for(int i = 0; i < s.length(); i++) {
56+
//在t中寻找
57+
char c = s.charAt(i);
58+
59+
boolean flag = false;
60+
for(int k = j ; k < nt; k++) {
61+
if(t.charAt(k) == c) {
62+
j = k+1;
63+
flag = true;
64+
break;
65+
}
66+
}
67+
68+
if(!flag) {
69+
return false;
70+
}
71+
}
72+
73+
return true;
74+
}
75+
```
76+
77+
## 效果
78+
79+
0ms 击败 100.00%
80+
81+
## 复杂度
82+
83+
TC: O(n)
84+
85+
## 反思
86+
87+
这个属于第一直觉直接解决了,还是多刷题比较好。
88+
89+
# 参考资料

0 commit comments

Comments
 (0)