From 3c3a623b8ac48c8df4a07b242fb218b8cf0427ce Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Mon, 20 Feb 2023 13:47:11 -0600 Subject: [PATCH 1/5] =?UTF-8?q?=E5=9C=A8=E5=9B=BE=E7=89=87=E5=89=8D?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=9B=9E=E8=BD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树理论基础.md | 1 + 1 file changed, 1 insertion(+) diff --git a/problems/二叉树理论基础.md b/problems/二叉树理论基础.md index 21e2039f..9fc6a39c 100644 --- a/problems/二叉树理论基础.md +++ b/problems/二叉树理论基础.md @@ -59,6 +59,7 @@ * 它的左、右子树也分别为二叉排序树 下面这两棵树都是搜索树 + From 0b3e4aed920c420d6fdb1d76872e826796926ed3 Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Mon, 20 Feb 2023 22:30:01 -0600 Subject: [PATCH 2/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=B0=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1207.独一无二的出现次数.md | 23 +++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/problems/1207.独一无二的出现次数.md b/problems/1207.独一无二的出现次数.md index afc93eda..1a7a0019 100644 --- a/problems/1207.独一无二的出现次数.md +++ b/problems/1207.独一无二的出现次数.md @@ -98,7 +98,8 @@ class Solution { ``` Python: -```python +```python +# 方法 1: 数组在哈西法的应用 class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: count = [0] * 2002 @@ -113,6 +114,26 @@ class Solution: return False return True ``` +```python +# 方法 2: map 在哈西法的应用 +class Solution: + def uniqueOccurrences(self, arr: List[int]) -> bool: + ref = dict() + + for i in range(len(arr)): + ref[arr[i]] = ref.get(arr[i], 0) + 1 + + value_list = sorted(ref.values()) + + for i in range(len(value_list) - 1): + if value_list[i + 1] == value_list[i]: + return False + + return True + +``` + + Go: JavaScript: From 97e5867fac9161200652c52dafa955df6805cdca Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Wed, 22 Feb 2023 22:15:19 +0800 Subject: [PATCH 3/5] =?UTF-8?q?Update=200090.=E5=AD=90=E9=9B=86II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0090.子集II.md | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/problems/0090.子集II.md b/problems/0090.子集II.md index 5df47986..a02161aa 100644 --- a/problems/0090.子集II.md +++ b/problems/0090.子集II.md @@ -440,7 +440,7 @@ impl Solution { let len = nums.len(); // if start_index >= len { return; } for i in start_index..len { - if i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false { continue; } + if i > 0 && nums[i] == nums[i - 1] && !used[i - 1] { continue; } path.push(nums[i]); used[i] = true; Self::backtracking(result, path, nums, i + 1, used); @@ -449,11 +449,10 @@ impl Solution { } } - pub fn subsets_with_dup(nums: Vec) -> Vec> { + pub fn subsets_with_dup(mut nums: Vec) -> Vec> { let mut result: Vec> = Vec::new(); let mut path: Vec = Vec::new(); let mut used = vec![false; nums.len()]; - let mut nums = nums; nums.sort(); Self::backtracking(&mut result, &mut path, &nums, 0, &mut used); result @@ -461,6 +460,35 @@ impl Solution { } ``` +set 去重版本 + +```rust +use std::collections::HashSet; +impl Solution { + pub fn subsets_with_dup(mut nums: Vec) -> Vec> { + let mut res = HashSet::new(); + let mut path = vec![]; + nums.sort(); + Self::backtracking(&nums, &mut path, &mut res, 0); + res.into_iter().collect() + } + + pub fn backtracking( + nums: &Vec, + path: &mut Vec, + res: &mut HashSet>, + start_index: usize, + ) { + res.insert(path.clone()); + for i in start_index..nums.len() { + path.push(nums[i]); + Self::backtracking(nums, path, res, i + 1); + path.pop(); + } + } +} +``` + ### C ```c From 42abe9d51fc8cc9feffb73e1d4d8308e22cca18b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8A=B1=E6=97=A0=E7=BC=BA?= Date: Wed, 22 Feb 2023 23:43:25 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E7=BA=A0=E6=AD=A30203.=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0.md=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E4=B8=AD=E6=B1=89=E5=AD=97=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0203.移除链表元素.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md index 01a6d891..88387667 100644 --- a/problems/0203.移除链表元素.md +++ b/problems/0203.移除链表元素.md @@ -5,7 +5,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

-> 链表操作中,可以使用原链表来直接进行删除操作,也可以设置一个虚拟头结点在进行删除操作,接下来看一看哪种方式更方便。 +> 链表操作中,可以使用原链表来直接进行删除操作,也可以设置一个虚拟头结点再进行删除操作,接下来看一看哪种方式更方便。 # 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: