File tree Expand file tree Collapse file tree 3 files changed +70
-1
lines changed Expand file tree Collapse file tree 3 files changed +70
-1
lines changed Original file line number Diff line number Diff line change
1
+
2
+ ## :pencil2 : 基础刷题之(392. Is Subsequence)
3
+ <br >.
4
+ ** 2020-07-15 星期二 开始吧 库里的深夜食堂**
5
+
6
+ ### :pencil2 : 描述
7
+ ** 给定字符串 s 和 t,判断 s 是否是 t 的子序列。子序列的意思是此字符串是由原字符不删或者删除一些中间字符而不改变原有的字符位置而形成的新字符串。**
8
+ ### :pencil2 : 题目实例
9
+
10
+ <a href =" https://github.com/wuqinqiang/ " >
11
+ <img src =" https://github.com/wuqinqiang/Lettcode-php/blob/master/images/392.png " >
12
+ </a >
13
+
14
+ ****
15
+ ### :pencil2 : 题目分析
16
+
17
+ ** 最直观的解法,常人思维,一一对比。直接遍历字符串 s 然后一一在 t 中对比,如果存在,就记录下此字符在 t 中的下标 index,等对比下一位字符的时候,就从 t 的 index +1 开始,只要一个不存在,说明 s 不是 t 的子序列。**
18
+
19
+ ### :pencil2 : 解法一
20
+
21
+
22
+ ``` php
23
+ /**
24
+ * @param String $s
25
+ * @param String $t
26
+ * @return Boolean
27
+ */
28
+ function isSubsequence($s, $t)
29
+ {
30
+ $index = 0;
31
+ for ($i = 0; $i < strlen($s); $i++) {
32
+ $index = strpos($t, $s[$i], $index);
33
+ if ($index === false) {
34
+ return false;
35
+ }
36
+ $index++;
37
+ }
38
+ return true;
39
+ }
40
+
41
+ ```
42
+
43
+ ### :pencil2 : 解法二
44
+ ** 时间复杂度上可以从 O(n2) 到 O(n)。 还是很常规的做法。可以用两个指针 i,j 分别表示 s 和 t 的位置,遍历字符串,只要当前位置两个字符串相等,那么 i 和 j 同时递增,否则 j 单独自增。最后只需要判断 i 是否等于 s 的长度即可。时间复杂度 O(n)。**
45
+ ``` php
46
+
47
+ /**
48
+ * @param String $s
49
+ * @param String $t
50
+ * @return Boolean
51
+ */
52
+ function isSubsequence($s, $t)
53
+ {
54
+ $i = 0;
55
+ for ($j = 0; $j < strlen($t) && $i < strlen($s); $j++) {
56
+ if ($s[$i] === $t[$j]) $i++;
57
+ }
58
+ return $i === strlen($s);
59
+ }
60
+
61
+ ```
62
+
63
+ #### 联系
64
+
65
+ <a href =" https://github.com/wuqinqiang/ " >
66
+ <img src =" https://github.com/wuqinqiang/Lettcode-php/blob/master/qrcode_for_gh_c194f9d4cdb1_430.jpg " width =" 150px " height =" 150px " >
67
+ </a >
Original file line number Diff line number Diff line change 224
224
- [ Leetcode374] ( https://github.com/wuqinqiang/Lettcode-php/blob/master/301-400/374.md )
225
225
- [ Leetcode378] ( https://github.com/wuqinqiang/Lettcode-php/blob/master/301-400/378.md )
226
226
- [ Leetcode371] ( https://github.com/wuqinqiang/Lettcode-php/blob/master/301-400/371.md )
227
+ - [ Leetcode392] ( https://github.com/wuqinqiang/Lettcode-php/blob/master/301-400/392.md )
227
228
****
228
229
229
- ** 501-600**
230
+ ** 401-600**
231
+ - [ Leetcode441] ( https://github.com/wuqinqiang/Lettcode-php/blob/master/501-600/441.md )
230
232
- [ Leetcode513] ( https://github.com/wuqinqiang/Lettcode-php/blob/master/501-600/513.md )
231
233
****
232
234
You can’t perform that action at this time.
0 commit comments