Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d48e48c

Browse files
🐱(double-pointer): 392. 判断子序列
1 parent 337cac6 commit d48e48c

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

‎docs/_sidebar.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* [位运算](algorithm/bit/)
2828
* [数学题](algorithm/math/)
2929
* [回溯](algorithm/backtrack/)
30+
* [双指针](algorithm/double-pointer/)
3031
* [其他](algorithm/other/)
3132
* 周赛
3233
* [第 121 场周赛](weekly/121/)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## 392. 判断子序列
2+
3+
[原题链接](https://leetcode-cn.com/problems/is-subsequence/)
4+
5+
### 思路
6+
7+
双指针。
8+
9+
指针 `i` 指向 `s` 第一个字符,指针 `j` 指向 `t` 第一个字符。逐一判断 `i` 所指向的字符是否在 `t` 中存在。
10+
11+
- 如果 `s[i] != t[j]`:`j = j + 1`,继续比对下一个字符
12+
- 如果 `s[i] == t[j]`:`i = i + 1`,`j = j + 1`,继续进行下一个 `s[i]` 的查找
13+
14+
```python
15+
class Solution(object):
16+
def isSubsequence(self, s, t):
17+
"""
18+
:type s: str
19+
:type t: str
20+
:rtype: bool
21+
"""
22+
s_length = len(s)
23+
t_length = len(t)
24+
25+
i = 0
26+
j = 0
27+
28+
while i < s_length:
29+
s_c = s[i]
30+
has = False
31+
while j < t_length:
32+
t_c = t[j]
33+
j += 1
34+
if s_c == t_c:
35+
i += 1
36+
has = True
37+
break
38+
39+
if has == False:
40+
return False
41+
42+
return True
43+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /