参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
-> 链表操作中,可以使用原链表来直接进行删除操作,也可以设置一个虚拟头结点在进行删除操作,接下来看一看哪种方式更方便。 +> 链表操作中,可以使用原链表来直接进行删除操作,也可以设置一个虚拟头结点再进行删除操作,接下来看一看哪种方式更方便。 # 203.移除链表元素 From e7bc3d3a7ee984f40153a270d0d42d461d5d2136 Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Wed, 22 Feb 2023 20:07:09 -0600 Subject: [PATCH 5/5] =?UTF-8?q?add=20python=E7=AE=97=E6=B3=95=E6=A8=A1?= =?UTF-8?q?=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/算法模板.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/problems/算法模板.md b/problems/算法模板.md index 24d3e6bd..59de69df 100644 --- a/problems/算法模板.md +++ b/problems/算法模板.md @@ -803,6 +803,46 @@ Java: Python: +## 二分查找法 +```python +def binarysearch(nums, target): + low = 0 + high = len(nums) - 1 + while (low <= high): + mid = (high + low)//2 + + if (nums[mid] < target): + low = mid + 1 + + if (nums[mid] > target): + high = mid - 1 + + if (nums[mid] == target): + return mid + + return -1 +``` + +## KMP + +```python +def kmp(self, a, s): + # a: length of the array + # s: string + + next = [0]*a + j = 0 + next[0] = 0 + + for i in range(1, len(s)): + while j > 0 and s[j] != s[i]: + j = next[j - 1] + + if s[j] == s[i]: + j += 1 + next[i] = j + return next +``` Go: