File tree 1 file changed +31
-0
lines changed
1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -769,6 +769,36 @@ class Solution:
769
769
return next
770
770
```
771
771
772
+ ``` python
773
+ // 前缀表(不减一)Python实现
774
+ class Solution :
775
+ def strStr (self , haystack : str , needle : str ) -> int :
776
+ if len (needle) == 0 :
777
+ return 0
778
+ next = self .getNext(needle)
779
+ j = 0
780
+ for i in range (len (haystack)):
781
+ while j >= 1 and haystack[i] != needle[j]:
782
+ j = next [j- 1 ]
783
+ if haystack[i] == needle[j]:
784
+ j += 1
785
+ if j == len (needle):
786
+ return i - len (needle) + 1
787
+ return - 1
788
+
789
+ def getNext (self , needle ):
790
+ next = [0 ] * len (needle)
791
+ j = 0
792
+ next [0 ] = j
793
+ for i in range (1 , len (needle)):
794
+ while j >= 1 and needle[i] != needle[j]:
795
+ j = next [j- 1 ]
796
+ if needle[i] == needle[j]:
797
+ j += 1
798
+ next [i] = j
799
+ return next
800
+ ```
801
+
772
802
Go:
773
803
774
804
``` go
@@ -1352,3 +1382,4 @@ impl Solution {
1352
1382
<a href =" https://programmercarl.com/other/kstar.html " target =" _blank " >
1353
1383
<img src =" ../pics/网站星球宣传海报.jpg " width =" 1000 " />
1354
1384
</a >
1385
+
You can’t perform that action at this time.
0 commit comments