mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2026-03-11 04:14:41 +00:00
添加了python版本代码
为数据结构和算法文件夹下的代码增加了python语言版本
This commit is contained in:
@@ -70,6 +70,8 @@
|
||||
|
||||
#### 题目代码
|
||||
|
||||
Java Code:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public int strStr(String haystack, String needle) {
|
||||
@@ -103,10 +105,37 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python Code:
|
||||
|
||||
```python
|
||||
from typing import List
|
||||
class Solution:
|
||||
def strStr(self, haystack: str, needle: str)->int:
|
||||
haylen = len(haystack)
|
||||
needlen = len(needle)
|
||||
# 特殊情况
|
||||
if haylen < needlen:
|
||||
return -1
|
||||
if needlen == 0:
|
||||
return 0
|
||||
# 主串
|
||||
for i in range(0, haylen - needlen + 1):
|
||||
# 模式串
|
||||
j = 0
|
||||
while j < needlen:
|
||||
if haystack[i + j] != needle[j]:
|
||||
break
|
||||
j += 1
|
||||
# 匹配成功
|
||||
if j == needlen:
|
||||
return i
|
||||
return -1
|
||||
```
|
||||
|
||||
我们看一下BF算法的另一种算法(显示回退),其实原理一样,就是对代码进行了一下修改,只要是看完咱们的动图,这个也能够一下就能看懂,大家可以结合下面代码中的注释和动图进行理解。
|
||||
|
||||
Java Code:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public int strStr(String haystack, String needle) {
|
||||
@@ -134,6 +163,32 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python Code:
|
||||
|
||||
```python
|
||||
from typing import List
|
||||
class Solution:
|
||||
def strStr(self, haystack: str, needle: str)->int:
|
||||
# i代表主串指针,j模式串
|
||||
i = 0
|
||||
j = 0
|
||||
# 主串长度和模式串长度
|
||||
halen = len(haystack)
|
||||
nelen = len(needle)
|
||||
# 循环条件,这里只有 i 增长
|
||||
while i < halen and j < nelen:
|
||||
# 相同时,则移动 j 指针
|
||||
if haystack[i] == needle[j]:
|
||||
j += 1
|
||||
else:
|
||||
# 不匹配时,将 j 重新只想模式串的头部,将 i 本次匹配的开始位置的下一字符
|
||||
i -= j
|
||||
j = 0
|
||||
i += 1
|
||||
# 查询成功时返回索引,查询失败时返回 -1
|
||||
renum = i - nelen if j == nelen else -1
|
||||
return renum
|
||||
```
|
||||
|
||||
|
||||
## BM算法(Boyer-Moore)
|
||||
@@ -262,6 +317,8 @@ BM 算法是从右往左进行比较,发现坏字符的时候此时 cac 已
|
||||
|
||||
这破图画起来是真费劲啊。下面我们来看一下算法代码,代码有点长,我都标上了注释也在网站上 AC 了,如果各位感兴趣可以看一下,不感兴趣理解坏字符和好后缀规则即可。可以直接跳到 KMP 部分
|
||||
|
||||
Java Code:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public int strStr(String haystack, String needle) {
|
||||
@@ -355,6 +412,89 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python Code:
|
||||
|
||||
```python
|
||||
from typing import List
|
||||
class Solution:
|
||||
def strStr(self, haystack: str, needle: str)->int:
|
||||
haylen = len(haystack)
|
||||
needlen = len(needle)
|
||||
return self.bm(haystack, haylen, needle, needlen)
|
||||
|
||||
# 用来求坏字符情况下移动位数
|
||||
def badChar(self, b: str, m: int, bc: List[int]):
|
||||
# 初始化
|
||||
for i in range(0, 256):
|
||||
bc[i] = -1
|
||||
# m 代表模式串的长度,如果有两个 a,则后面那个会覆盖前面那个
|
||||
for i in range(0, m,):
|
||||
ascii = ord(b[i])
|
||||
bc[ascii] = i# 下标
|
||||
|
||||
# 用来求好后缀条件下的移动位数
|
||||
def goodSuffix(self, b: str, m: int, suffix: List[int], prefix: List[bool]):
|
||||
# 初始化
|
||||
for i in range(0, m):
|
||||
suffix[i] = -1
|
||||
prefix[i] = False
|
||||
for i in range(0, m - 1):
|
||||
j = i
|
||||
k = 0
|
||||
while j >= 0 and b[j] == b[m - 1 - k]:
|
||||
j -= 1
|
||||
k += 1
|
||||
suffix[k] = j + 1
|
||||
if j == -1:
|
||||
prefix[k] = True
|
||||
|
||||
def bm(self, a: str, n: int, b: str, m: int)->int:
|
||||
bc = [0] * 256# 创建一个数组用来保存最右边字符的下标
|
||||
self.badChar(b, m, bc)
|
||||
# 用来保存各种长度好后缀的最右位置的数组
|
||||
suffix_index = [0] * m
|
||||
# 判断是否是头部,如果是头部则True
|
||||
ispre = [False] * m
|
||||
self.goodSuffix(b, m, suffix_index, ispre)
|
||||
i = 0# 第一个匹配字符
|
||||
# 注意结束条件
|
||||
while i <= n - m:
|
||||
# 从后往前匹配,匹配失败,找到坏字符
|
||||
j = m - 1
|
||||
while j >= 0:
|
||||
if a[i + j] != b[j]:
|
||||
break
|
||||
j -= 1
|
||||
# 模式串遍历完毕,匹配成功
|
||||
if j < 0:
|
||||
return i
|
||||
# 下面为匹配失败时,如何处理
|
||||
# 求出坏字符规则下移动的位数,就是我们坏字符下标减最右边的下标
|
||||
x = j - bc[ord(a[i + j])]
|
||||
y = 0
|
||||
# 好后缀情况,求出好后缀情况下的移动位数,如果不含有好后缀的话,则按照坏字符来
|
||||
if y < m - 1 and m - 1 - j > 0:
|
||||
y = self.move(j, m, suffix_index, ispre)
|
||||
# 移动
|
||||
i += max(x, y)
|
||||
return -1
|
||||
|
||||
# j代表坏字符的下标
|
||||
def move(j: int, m: int, suffix_index: List[int], ispre: List[bool])->int:
|
||||
# 好后缀长度
|
||||
k = m - 1 - j
|
||||
# 如果含有长度为 k 的好后缀,返回移动位数
|
||||
if suffix_index[k] != -1:
|
||||
return j - suffix_index[k] + 1
|
||||
# 找头部为好后缀子串的最大长度,从长度最大的子串开始
|
||||
for r in range(j + 2, m):
|
||||
# //如果是头部
|
||||
if ispre[m - r] == True:
|
||||
return r
|
||||
# 如果没有发现好后缀匹配的串,或者头部为好后缀子串,则移动到 m 位,也就是匹配串的长度
|
||||
return m
|
||||
```
|
||||
|
||||
我们来理解一下我们代码中用到的两个数组,因为两个规则的移动位数,只与模式串有关,与主串无关,所以我们可以提前求出每种情况的移动情况,保存到数组中。
|
||||
|
||||

|
||||
@@ -413,6 +553,8 @@ next 数组存的咱们最长公共前后缀中,前缀的结尾字符下标。
|
||||
|
||||
**注:很多教科书的 next 数组表示方式不一致,理解即可**
|
||||
|
||||
Java Code:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public int strStr(String haystack, String needle) {
|
||||
@@ -486,5 +628,64 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python Code:
|
||||
|
||||
```python
|
||||
from typing import List
|
||||
class Solution:
|
||||
def strStr(self, haystack: str, needle: str)->int:
|
||||
# 两种特殊情况
|
||||
if len(needle) == 0:
|
||||
return 0
|
||||
if len(haystack) == 0:
|
||||
return -1
|
||||
# 长度
|
||||
halen = len(haystack)
|
||||
nelen = len(needle)
|
||||
# 返回下标
|
||||
return self.kmp(haystack, halen, needle, nelen)
|
||||
|
||||
def kmp(self, hasyarr: str, halen: int, nearr: str, nelen: int)->int:
|
||||
# 获取next 数组
|
||||
next = self.next(nearr, nelen)
|
||||
j = 0
|
||||
for i in range(0, halen):
|
||||
# 发现不匹配的字符,然后根据 next 数组移动指针,移动到最大公共前后缀的,
|
||||
# 前缀的后一位,和咱们移动模式串的含义相同
|
||||
while j > 0 and hasyarr[i] != nearr[j]:
|
||||
j = next[j - 1] + 1
|
||||
# 超出长度时,可以直接返回不存在
|
||||
if nelen - j + i > halen:
|
||||
return -1
|
||||
# 如果相同就将指针同时后移一下,比较下个字符
|
||||
if hasyarr[i] == nearr[j]:
|
||||
j += 1
|
||||
# 遍历完整个模式串,返回模式串的起点下标
|
||||
if j == nelen:
|
||||
return i - nelen + 1
|
||||
return -1
|
||||
|
||||
# 这一块比较难懂,不想看的同学可以忽略,了解大致含义即可,或者自己调试一下,看看运行情况
|
||||
# 我会每一步都写上注释
|
||||
def next(self, needle: str, len:int)->List[int]:
|
||||
# 定义 next 数组
|
||||
next = [0] * len
|
||||
# 初始化
|
||||
next[0] = -1
|
||||
k = -1
|
||||
for i in range(1, len):
|
||||
# 我们此时知道了 [0,i-1]的最长前后缀,但是k+1的指向的值和i不相同时,我们则需要回溯
|
||||
# 因为 next[k]就时用来记录子串的最长公共前后缀的尾坐标(即长度)
|
||||
# 就要找 k+1前一个元素在next数组里的值,即next[k+1]
|
||||
while k != -1 and needle[k + 1] != needle[i]:
|
||||
k = next[k]
|
||||
# 相同情况,就是 k的下一位,和 i 相同时,此时我们已经知道 [0,i-1]的最长前后缀
|
||||
# 然后 k - 1 又和 i 相同,最长前后缀加1,即可
|
||||
if needle[k + 1] == needle[i]:
|
||||
k += 1
|
||||
next[i] = k
|
||||
return next
|
||||
```
|
||||
|
||||
这篇文章真的写了很久很久,觉得还不错的话,就麻烦您点个赞吧,大家也可以去我的公众号看我的所有文章,每个都有动图解析,公众号:[袁厨的算法小屋](https://cdn.jsdelivr.net/gh/tan45du/tan45du.github.io.photo@master/photo/qrcode_for_gh_1f36d2ef6df9_258.5lojyphpkso0.jpg)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user