File tree 3 files changed +111
-0
lines changed
3 files changed +111
-0
lines changed Original file line number Diff line number Diff line change 28
28
* [ 25. Reverse Nodes in k-Group] ( leetCode-25-Reverse-Nodes-in-k-Group.md )
29
29
* [ 26. Remove Duplicates from Sorted Array] ( leetCode-26-Remove-Duplicates-from-Sorted-Array.md )
30
30
* [ 27. Remove Element] ( leetCode-27-Remove-Element.md )
31
+ * [ 28. Implement strStr()] ( leetCode-28-Implement-strStr.md )
31
32
* [ 79. Word Search] ( leetCode-79-Word-Search.md )
Original file line number Diff line number Diff line change
1
+ # 题目描述(简单难度)
2
+
3
+ ![ ] ( https://windliang.oss-cn-beijing.aliyuncs.com/28.jpg )
4
+
5
+ 返回一个字符串 needle 在另一个字符串 haystack 中开始的位置,如果不存在就返回 -1 ,如果 needle 长度是 0 ,就返回 0 。
6
+
7
+ 就是一一比较就好,看下代码吧。
8
+
9
+ ``` java
10
+ public int strStr(String haystack, String needle) {
11
+ if (needle. length() == 0 ) {
12
+ return 0 ;
13
+ }
14
+ int j = 0 ;
15
+ // 遍历每个字符
16
+ for (int i = 0 ; i < haystack. length(); i++ ) {
17
+ // 相等的话计数加 1
18
+ if (haystack. charAt(i) == needle. charAt(j)) {
19
+ j++ ;
20
+ // 长度够了就返回
21
+ if (j == needle. length()) {
22
+ return i - j + 1 ;
23
+ }
24
+ // 不相等的话 j 清零,
25
+ // 并且 i 回到最初的位置,之后就进入 for 循环中的 i++,从下个位置继续判断
26
+ } else {
27
+ i = i - j;
28
+ j = 0 ;
29
+ }
30
+ }
31
+ return - 1 ;
32
+ }
33
+ ```
34
+
35
+ 时间复杂度:假设 haystack 和 needle 的长度分别是 n 和 k,对于每一个 i ,我们最多执行 k - 1 次,总共会有 n 个 i ,所以时间复杂度是 O(kn)。
36
+
37
+ 空间复杂度:O(1)。
38
+
39
+ 我们再看下别人的[ 代码] ( https://leetcode.com/problems/implement-strstr/discuss/12807/Elegant-Java-solution ) ,用两个 for 循环。但本质其实是一样的,但可能会更好理解些吧。
40
+
41
+ ``` java
42
+ public int strStr(String haystack, String needle) {
43
+ for (int i = 0 ; ; i++ ) {
44
+ for (int j = 0 ; ; j++ ) {
45
+ if (j == needle. length()) return i;
46
+ if (i + j == haystack. length()) return - 1 ;
47
+ if (needle. charAt(j) != haystack. charAt(i + j)) break ;
48
+ }
49
+ }
50
+ }
51
+ ```
52
+
53
+ # 总
54
+
55
+ 总的来说,还是比较简单的,就是简单的遍历就实现了。
Original file line number Diff line number Diff line change
1
+ # 题目描述(简单难度)
2
+
3
+ ![ ] ( https://windliang.oss-cn-beijing.aliyuncs.com/28.jpg )
4
+
5
+ 返回一个字符串 needle 在另一个字符串 haystack 中开始的位置,如果不存在就返回 -1 ,如果 needle 长度是 0 ,就返回 0 。
6
+
7
+ 就是一一比较就好,看下代码吧。
8
+
9
+ ``` java
10
+ public int strStr(String haystack, String needle) {
11
+ if (needle. length() == 0 ) {
12
+ return 0 ;
13
+ }
14
+ int j = 0 ;
15
+ // 遍历每个字符
16
+ for (int i = 0 ; i < haystack. length(); i++ ) {
17
+ // 相等的话计数加 1
18
+ if (haystack. charAt(i) == needle. charAt(j)) {
19
+ j++ ;
20
+ // 长度够了就返回
21
+ if (j == needle. length()) {
22
+ return i - j + 1 ;
23
+ }
24
+ // 不相等的话 j 清零,
25
+ // 并且 i 回到最初的位置,之后就进入 for 循环中的 i++,从下个位置继续判断
26
+ } else {
27
+ i = i - j;
28
+ j = 0 ;
29
+ }
30
+ }
31
+ return - 1 ;
32
+ }
33
+ ```
34
+
35
+ 时间复杂度:假设 haystack 和 needle 的长度分别是 n 和 k,由于不匹配的时候,i 会回到开始匹配的时候的位置,所以每一个 i 都会执行到,总共会有 n 个 i ,并且对于每个 i 最多执行 k - 1 次,所以时间复杂度是 O(kn)。
36
+
37
+ 空间复杂度:O(1)。
38
+
39
+ 我们再看下别人的[ 代码] ( https://leetcode.com/problems/implement-strstr/discuss/12807/Elegant-Java-solution ) ,用两个 for 循环。但本质其实是一样的,但可能会更好理解些吧。
40
+
41
+ ``` java
42
+ public int strStr(String haystack, String needle) {
43
+ for (int i = 0 ; ; i++ ) {
44
+ for (int j = 0 ; ; j++ ) {
45
+ if (j == needle. length()) return i;
46
+ if (i + j == haystack. length()) return - 1 ;
47
+ if (needle. charAt(j) != haystack. charAt(i + j)) break ;
48
+ }
49
+ }
50
+ }
51
+ ```
52
+
53
+ # 总
54
+
55
+ 总的来说,还是比较简单的,就是简单的遍历就实现了。
You can’t perform that action at this time.
0 commit comments