File tree Expand file tree Collapse file tree 1 file changed +7
-4
lines changed Expand file tree Collapse file tree 1 file changed +7
-4
lines changed Original file line number Diff line number Diff line change @@ -20,18 +20,21 @@ def build_next():
20
20
21
21
origin_index , pattern_index = 0 , 0
22
22
next_list = build_next ()
23
+ print (next_list )
23
24
while origin_index < origin_len :
24
- if pattern [pattern_index ] == origin [origin_index ]:
25
- pattern_index += 1
26
- origin_index += 1
25
+ # while需要放在前面,如果放在后面的话且有匹配的情况下pattern[pattern_index]就会越界
27
26
while pattern_index > 0 and origin [origin_index ] != pattern [pattern_index ]:
28
27
pattern_index = next_list [pattern_index ]
28
+ if pattern [pattern_index ] == origin [origin_index ]:
29
+ pattern_index += 1
30
+ origin_index += 1
31
+
29
32
if pattern_index == pattern_len :
30
33
return origin_index - pattern_len
31
34
32
35
33
36
def main ():
34
- print match ("sb " , 'sb' )
37
+ print match ("assssbsss " , 'sb' )
35
38
36
39
37
40
if __name__ == "__main__" :
You can’t perform that action at this time.
0 commit comments